alex.melnyk alex.melnyk - 7 months ago 30
Scala Question

Scala method call parameters with colon

final def apply(block: => Result): Action[AnyContent] =
apply(BodyParsers.utils.ignore(AnyContentAsEmpty: AnyContent))(_ => block)

Does Anybody know what does this
AnyContentAsEmpty: AnyContent
mean ? Particularly
: AnyContent

Answer Source

Does Anybody know what does this AnyContentAsEmpty: AnyContent mean ?

It is a Typed Expression.

Particularly : AnyContent ?

This is a Type Ascription, which is used to ascribe a type to an expression, thus making the expression a typed expression.

Normally, Scala computes the types of expressions without your help, but you can ascribe a specific type to an expression, if you don't like the one that Scala computes.

For example, for the expression


Scala computes the type Int. You could also write

42: Int

instead, and you would get the same result. If you don't want 42 to be typed as Int, you can ascribe a different type to it, e.g.:

42: Long

Now, the expression has type Long instead of Int. If you were to assign it to a val, for example, and don't provide a type for the val, Scala would infer type 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

You can use this to guide Scala to infer a more sensible type, or to pick a particular overload.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download