optional
optional

Reputation: 3350

var initalization at the time of declaration in scala

I am confused with following initialization

var in = None: Option[FileInputStream]

however what I know is that

var varName : type = _ // default value initialization
var varName : type = someValue // other than default intitalization

but what is

 var in = None: Option[FileInputStream] 

Please help Thanks

Upvotes: 0

Views: 62

Answers (2)

Jörg W Mittag
Jörg W Mittag

Reputation: 369594

This is called a type ascription and the resulting expression is called a typed expression. It, well, ascribes a type to an expression:

expr: Type

means "treat expr as if it had Type".

For example:

1
// Int

1: Float
// Float

Obviously, the expression needs to conform to the type that is ascribed to it.

The most widely used example of type ascription is probably the _* pseudo-type, which unpacks a Seq into a series of arguments:

def sumNums(nums: Int*) = nums.sum

sumNums()
//=> 0

sumNums(1)
//=> 1

sumNums(1, 2)
//=> 3

sumNums(Seq(1, 2))
// error: type mismatch;
// found   : Seq[Int]
// required: Int
//           sumNums(Seq(1, 2))
//                      ^

sumNums(Seq(1, 2): _*)
//=> 3

In your particular case, I find it questionable to ascribe a type to an expression just to get the type inferencer to infer the correct type, when you could just as well have declared the correct type to begin with:

var in: Option[FileInputStream] = None

With regards to your comments:

however what I know is that

var varName : type = _ // default value initialization
var varName : type = someValue // other than default intitalization

You can also leave out the type:

var varName = someValue

In this case, the type is inferred from the expression someValue.

var varName = _

Obviously, this cannot work: the type is inferred from the expression, but the value of the expression is inferred from the type. Therefore, this is not allowed.

Your example uses the form with inferred type:

var in = someExpression

So, the type of in is inferred from the type of someExpression.

Now, if we said

var in = None

then the type inferred for in would be None.type, i.e. the singleton type of the None object. But that doesn't make sense: why would we have a var, i.e. a variable which we can change, when we then give it a singleton type, i.e. an type which has only a single instance. So, we can re-assign in as often as we want, but we can only assign the same thing to it. That's illogical. (Well, technically, we could also assign null to it.)

And in fact, we want to be able to assign something to it, but also know whether we assigned something to it or not. That's why we use an Option in this case, and initialize it with None. So, re-interpret None as an Option, so that Scala infers the correct type:

var in = None: Option[FileInputStream]

Upvotes: 4

Tzach Zohar
Tzach Zohar

Reputation: 37852

//  [name]  =  [value] : [type]
var in      =  None    : Option[FileInputStream] 

// can equivalently be written as: 
var in: Option[FileInputStream] = None    

This creates a variable of type Option[FileInputStream], with the initial value None. To learn more about Scala's Option type, see http://www.scala-lang.org/api/current/#scala.Option

Upvotes: 2

Related Questions