2017-08-20 35 views
1
final def apply(block: => Result): Action[AnyContent] = 
    apply(BodyParsers.utils.ignore(AnyContentAsEmpty: AnyContent))(_ => block) 

是否有人知道這是什麼AnyContentAsEmpty: AnyContent是什麼意思?特別: AnyContent帶冒號的Scala方法調用參數

回答

3

有誰知道這是什麼意思AnyContentAsEmpty: AnyContent

這是一個Typed Expression

特別是: AnyContent

這是一個Type Ascription,它用於將一個類型賦給一個表達式,從而使該表達式成爲一個類型化的表達式。

通常情況下,斯卡拉計算類型的表達式沒有你的幫助,但你可以歸咎於特定類型的表達式,如果你不喜歡斯卡拉計算的一個。

例如,對於表達

42 

Scala的計算類型Int。你也可以寫

42: Int 

取而代之,你會得到相同的結果。如果您希望42以分型爲Int,可以歸於不同類型的吧,例如:

42: Long 

現在,表達的類型爲Long,而不是Int。如果你把它分配給val,例如,不爲val提供一個類型,斯卡拉會推斷Long類型:

val a  = 1  // 1 has type Int, therefore Scala infers type Int for a 
val b: Long = 1  // b is typed as Long, therefore Scala converts 1 to a Long 
val c  = 1: Long // 1 is typed as Long, therefore Scala infers type Long for c 

你可以用這個來引導斯卡拉推斷出一個更明智的類型,或挑選特定的超載。

0

表達式expr: Type僅表示值expr,但是其類型爲Type。這在指導類型推斷中很有用。也就是說,表達

AnyContentAsEmpty 

已鍵入

AnyContentAsEmpty.type 

AnyContentAsEmpty: AnyContent 

已鍵入

AnyContent 

我不知道爲什麼它在這裏被使用,但也許你可以刪除: AnyContent看看 怎麼了。

該語法(稱爲類型歸屬)有兩個其他用途:隱式轉換和可變參數。另外,還要注意它是如何的相似變量聲明/ case聲明(val x: Any = ???/case x: String => ???)。

class Base 
class Extn 
implicit def Base2Extn(b: Base) = new Extn 
val x = new Base // x has type Base 
val y = new Base: Extn // y is Base2Extn(new Base), with type Extn 

def f(args: Any*): Unit = args match { 
    // `tail: _*` is like saying "use tail as a bunch of arguments instead of just one" 
    // Bit of a stretch but I think that's why the syntax was chosen 
    case head +: tail => { println(head); f(tail: _*) } 
    case _ =>() 
}