Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created September 5, 2012 01:36
Show Gist options
  • Save tonymorris/3628955 to your computer and use it in GitHub Desktop.
Save tonymorris/3628955 to your computer and use it in GitHub Desktop.
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
Copy link

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

@mergeconflict
Copy link

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
Copy link

Rúnar beat me to it :P

@retronym
Copy link

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