Haixu Huang
Posts: 6
Nickname: regular
Registered: Oct, 2008
|
|
Re: Scala is not friendly with space, and if-without-else
|
Posted: Dec 16, 2008 8:49 PM
|
|
> > Also, version 6 of Book omited the discuss of > difference > > between > > if (true | false) value > > and > > if (true | false) value1 else value2 > > > > Does it mean anything? In Scala v 2.7.2, the first one > > still produce Unit no matter the expression is true or > > false, will it be changed in next version? Waiting for > the > > answer here. > > If you use if as an expression that returns a value, the > inferred type of the expression is the least upper bound > of the "then" and "else" clauses. Since the else clause > doesn't return a value, Unit is the LUB. If you use an if > imperatively (e.g. if (condition) mutateMyState()) > everything will work as you'd expect because you don't > care about the return value anyway.
Don't think so. try this following code in interpretor:
val a = if (true) "a" else ()
the shell will reply:
a: Any = a
I think the only thing needed is "else ()", or maybe "else new Exception", but should be implicited.
|
|