Scala expression evaluation [confusing]

874 views
Skip to first unread message

Maatary Okouya

unread,
Apr 21, 2014, 5:20:54 PM4/21/14
to scala...@googlegroups.com

I'm intrigued by the following Scala compiler behavior. When i declare a function of type unit, but nevertheless provide as body a function that evaluate to an Int, the Scala compiler is ok with it.

def add(x:Int, y:Int) = x + y
def main(args: Array[String]): Unit = {add(args(0).toInt, args(0).toInt)}

While the same is not true with other type such in

def afunc: String = {1} //type mismatch;  found   : Int(1)  required: String

Also if i write

def afunc: Unit = {1}

or

def main(args: Array[String]): Unit = {2 + 2} // Which is just like the first addition above

In both case i get the following warning:

a pure expression does nothing in statement position; you may be omitting necessary parentheses 

In a sense there is 2 questions here. What is the difference between a function that return evaluate to an Int and the expression 2 + 2. Then why on earth, the compiler does not complain that a function that is suppose to evaluate to Unit, gets a body that evaluate to another type, as it would happens between other types.

Many thanks in advance,

Maatari

Som Snytt

unread,
Apr 21, 2014, 7:46:47 PM4/21/14
to Maatary Okouya, scala-user
for how a non-Unit value is handled when Unit is expected.

I supposed it's called value discarding instead of Unit-value insertion because the interesting value is not used, hence the warning.

For the case of the function call, the function could have side-effects, so it doesn't annoy with the warning.  I think the pure expr warning is geared for when something is on a separate line by accident.

scala> :se -Ywarn-value-discard:true

scala> def main(args: Array[String]): Unit = {2 + 2}
<console>:7: warning: discarded non-Unit value

       def main(args: Array[String]): Unit = {2 + 2}
                                                ^
<console>:7: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses

       def main(args: Array[String]): Unit = {2 + 2}
                                                ^




--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Maso

unread,
Apr 21, 2014, 8:04:25 PM4/21/14
to Som Snytt, Maatary Okouya, scala-user
I've always thought of it like this, for mental convenience (simpler
than the official language spec):

* There is an implicit conversion of any type of value to the Unit
type. The conversion is simple: the conversion ignores the value, and
returns the Unit value instead.
* Any time a Unit type is required for a block expression, the final
expression can be implicitly converted to the Unit value.

So the following method in, however its actually implemented in the
Scala compiler, has the same effect as apply the implicit Unit
conversion from Int in the following:

def myFunc(args: Array[String]): Unit = {2 + 2}


Again, that's completely an invention of my own. I find its easier to
reason about what the Scala compiler does if I pretend this conversion
exists.

Brian Maso
--
Best regards,
Brian Maso
(949) 395-8551
Follow me: @bmaso
br...@blumenfeld-maso.com

Maatary Okouya

unread,
Apr 21, 2014, 8:10:53 PM4/21/14
to scala...@googlegroups.com, Maatary Okouya
Hi, 

thanks for your help. Although what to you mean by <<<<I think the pure expr warning is geared for when something is on a separate line by accident.>>>

Som Snytt

unread,
Apr 21, 2014, 8:16:21 PM4/21/14
to Maatary Okouya, scala-user
I just meant, for example:

scala> :pa
// Entering paste mode (ctrl-D to finish)

{
  1
 +1
}

// Exiting paste mode, now interpreting.

<console>:9: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
                1
                ^
res0: Int = 1


Here, I did omit parentheses.

Maatary Okouya

unread,
Apr 22, 2014, 5:57:29 PM4/22/14
to scala...@googlegroups.com, Maatary Okouya
Many thanks, got it !!!

Maatary Okouya

unread,
Apr 22, 2014, 5:57:47 PM4/22/14
to scala...@googlegroups.com, Som Snytt, Maatary Okouya
Thanks for the input. 
Reply all
Reply to author
Forward
0 new messages