Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala WTF?
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")
}
@runarorama

This comment has been minimized.

Copy link

@runarorama runarorama commented Sep 5, 2012

It's taking you to mean X.apply[Unit]{ "abc"; () }

@mergeconflict

This comment has been minimized.

Copy link

@mergeconflict mergeconflict commented Sep 5, 2012

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; () }"

@mergeconflict

This comment has been minimized.

Copy link

@mergeconflict mergeconflict commented Sep 5, 2012

Rúnar beat me to it :P

@retronym

This comment has been minimized.

Copy link

@retronym retronym commented Sep 5, 2012

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