[scala] Confusion about Unit

View: New views
2 Messages — Rating Filter:   Alert me  

[scala] Confusion about Unit

by Alan Wood-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm confused about Unit. It seems that any type conforms to it even though it's populated by a single value? ...

scala> var a : Int = 24
a: Int = 24

scala> var u : Unit = ()
u: Unit = ()

scala> a = u
<console>:6: error: type mismatch;
 found   : Unit
 required: Int
       a = u
           ^

scala> u = a
u: Unit = ()

----
Alan

Re: [scala] Confusion about Unit

by Miles Sabin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 9, 2009 at 1:30 PM, Alan Wood<wood@...> wrote:
> I'm confused about Unit. It seems that any type conforms to it even though
> it's populated by a single value? ...
<snip/>

> scala> u = a
> u: Unit = ()

Predef defines an implicit conversion from Any to Unit, primarily to
allow the discarding of values of blocks with Unit as their expected
type. Convertibility via an implicit isn't quite the same as a subtype
relationship however ...

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin