Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Last active December 10, 2015 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puffnfresh/4465577 to your computer and use it in GitHub Desktop.
Save puffnfresh/4465577 to your computer and use it in GitHub Desktop.
Define my own Unit which doesn't obey the crazy "value discarding" Scala rules.
import language.implicitConversions
object Main {
object Unit
type Unit = Unit.type
implicit def unit2scalaunit(a: Unit): scala.Unit = ()
implicit def scalaunit2unit(a: scala.Unit): Unit = Unit
def printHelloWorld: Unit = {
println("Hello World")
// Won't compile
// 100
}
def main(args: Array[String]) {
printHelloWorld
printHelloWorld
}
}
@retronym
Copy link

retronym commented Jan 6, 2013

% scala -Ywarn-value-discard 
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def foo: Unit = 10
<console>:7: warning: discarded non-Unit value
       def foo: Unit = 10
                       ^
foo: Unit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment