Created
September 5, 2012 01:36
-
-
Save tonymorris/3628955 to your computer and use it in GitHub Desktop.
Scala WTF?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class X[A](a: A) | |
object X { | |
// succeeds | |
def q: X[String] = X("abc") | |
// succeeds | |
def r: X[Unit] = X("abc") | |
// fails | |
// def s: X[Unit] = q | |
// fails | |
// def t: X[Unit] = X[String]("abc") | |
// succeeds | |
def s: X[Unit] = X[Unit]("abc") | |
} |
Rúnar beat me to it :P
scala29 -Ywarn-value-discard -e 'case class X[A](a: A); def r: X[Unit] = X("abc")'
/var/folders/xq/4pqxykpn5n56mngmch4p5xgw0000gn/T/scalacmd5125780711720939650.scala:1: warning: discarded non-Unit value
case class X[A](a: A); def r: X[Unit] = X("abc")
^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala Language Spec §6.26.1 (Value Conversions): "If e has some value type and the expected type is
Unit
, e is converted to the expected type by embedding it in the term{ e; () }
"