The Artima Developer Community
Sponsored Link

Programming in Scala Forum
"if without an else" return value

5 replies on 1 page. Most recent reply: Jun 19, 2008 12:20 AM by Dirk Detering

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Jeff Heon

Posts: 40
Nickname: jfheon
Registered: Feb, 2005

"if without an else" return value Posted: May 23, 2008 1:40 PM
Reply to this message Reply
Advertisement
I'm going through the chapter "Built-in Control Structures", and I find it a bit wierd that an "if without an else" would always return Unit, even if the condition evaluates to true.

From the book:

scala> val b = if (true) "hi"
b: Unit = ()


I would have expected to have b equals to "hi", just as if I had written this:

scala> val b = if (true) "hi" else ()
b: Any = "hi"


An "if without an else" with condition evaluating to false would return () or maybe type Nothing.

I'm sure there's a logical reason for it, I'm just wondering what it is 8)

I didn't use the suggest link, as the book is correct as it is. It is just language design curiosity on my part.


Jeff Heon

Posts: 40
Nickname: jfheon
Registered: Feb, 2005

Re: "if without an else" return value Posted: May 26, 2008 7:31 AM
Reply to this message Reply
I've given it some thought, I and think I was too caught up in the functional minding for the case of an "if without an else."

I now realize an "if without an else" would only be used for side-effects, hence I have no use of a returning value, even when the condition is evaluated to true.

Kean Heng Lim

Posts: 5
Nickname: 53770
Registered: Jan, 2008

Re: "if without an else" return value Posted: Jun 7, 2008 8:32 AM
Reply to this message Reply
scala> val ifelse=if(true) "yes" else "no"
ifelse: java.lang.String = yes

scala> val ifonly=if (true) "yes"
ifonly: Unit = ()

The result type doesn't look intuitive to me. I think it's a special case in Scala.

Doesn't it Scala try to minimize the number of "special" case with compare to java? Any reason why String or other type (depends on the statement) is not as good as Unit?

Haeil Yi

Posts: 1
Nickname: 56142
Registered: Jun, 2008

Re: "if without an else" return value Posted: Jun 16, 2008 1:00 PM
Reply to this message Reply
On val b = if (true) "hi" else (), b can be String or Unit. So the scala interpreter determine the type of b is Any.

But, on val b = if (true) "hi", the type of b is the type of return value for "if without else" - Unit.

It's not wierd.

Callum Rhodes

Posts: 3
Nickname: 54709
Registered: Mar, 2008

Re: "if without an else" return value Posted: Jun 16, 2008 4:38 PM
Reply to this message Reply
One of the confusing things about an if without else is:

if (true)
if (false)
println("yes")
else println("no")

will print "no" where you would think it would print nothing. Maybe this is something to avoid?

Dirk Detering

Posts: 16
Nickname: det
Registered: Jul, 2005

Re: "if without an else" return value Posted: Jun 19, 2008 12:20 AM
Reply to this message Reply
> One of the confusing things about an if without else
> is:

> if (true)
> if (false)
> println("yes")
> else println("no")

> will print "no" where you would think it would print
> nothing. Maybe this is something to avoid?

Well, perhaps it is confusing for someone thinking in Python style, as he would expect the 'else' to belong to the first 'if' only due to indentation.

As a Java coder, reading that example immediately "wrong indentation" popped up in mind instead of "unexpected result".

This is in the same category of error like:

assert(50 == 7 + 3 * 5)


The rule of innermost relation of if-else is as "suprising" as the rules for operator precedence.
And as with setting parens in the arithmetic expression above, the right way to express the intented deflexion from the rule is indeed:


if (true) {
if (false)
println("yes")
}
else println("no")


Has more to do with code style. That leads to company style guides รก la : "always use braces" - even if it produces more optical noise.

Only one example for formatting, not Java standard, but expressive:

if (true)
{ if (false)
{ println ("yes") } }
else
{ println ("no") }

Flat View: This topic has 5 replies on 1 page
Topic: Symbols? Previous Topic   Next Topic Topic: I can't buy your book. The licesne is absurd.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use