0

How to explicitly set an Option which is a tuple of a certain type to be None?

scala> var c = Some(1,1)
c: Some[(Int, Int)] = Some((1,1))

scala> c = None
<console>:11: error: type mismatch;
 found   : None.type
 required: Some[(Int, Int)]
       c = None
           ^

scala> None()
<console>:11: error: None.type does not take parameters
       None()
           ^

scala> c = None()
<console>:11: error: None.type does not take parameters
       c = None()
               ^

scala> c = None.Int
<console>:11: error: value Int is not a member of object None
       c = None.Int
                ^

scala> c = None(Int, Int)
<console>:11: error: None.type does not take parameters
       c = None(Int, Int)
               ^

scala> c = (None, None)
<console>:11: error: type mismatch;
 found   : (None.type, None.type)
 required: Some[(Int, Int)]
       c = (None, None)
joesan
  • 13,963
  • 27
  • 95
  • 232
  • 4
    Just declare the type explicitly at first usage. The inference is too precise/restrictive here - Some instead of Option. var c: Option[(Int, Int)] = Some((1,1)) – Didier Dupont Nov 02 '15 at 14:45

3 Answers3

10

As @DidierDupont pointed it out - your first declaration of c infers that its type is Some[(Int, Int)], instead of Option[(Int, Int)]. Declare c as the following and you should be fine:

var c: Option[(Int, Int)] = Some((1, 1))

or, as Gabriele Petronella pointed out, simply

var c = Option((1, 1))
Zoltán
  • 21,321
  • 14
  • 93
  • 134
2

You declared c = Some(1,1) which set the type to Some. You really wanted a type of Option, so try by declaring the type:

var c:Option[(Int,Int)] = Some(1,1)
c = None
Keith Pinson
  • 1,725
  • 1
  • 14
  • 17
0

Actually this is not about tuples. Following code will not work:

var a = Some("String")
a = None

because Scala has a type Some[T] which is a subclass of Option[T] just like None, this means Some[T] and None types are at same inheritance level. So from this we can say that declaring type explicitly as others mentioned will do work.

var a: Option[(Int, Int)] = Some((1, 1))

you can check more if you want at: Scala Option

Hüseyin Zengin
  • 1,216
  • 11
  • 23