I found this statement:
var o = None: Option[String]
that you can use to set o to a None, with a view to later maybe setting it to a Some[String]
But how does this actual statement break down syntactically? None is an object that extends Option[Nothing] - but how does the rest of the statement work? For example, what does the colon do?
Many thanks!
In scala, you can follow any expression with a type ascription. Like 1: Int
is totally valid. So it's really:
var o = (None: Option[String])
The purpose is to tell the compiler that that None
should be typed as an Option[String]
, so that o
isn't typed as None.type
. Basically, in this example, it's the same as:
var o: Option[String] = None
More here: https://stackoverflow.com/a/2087356/247985
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.