Created
September 9, 2012 06:48
-
-
Save dlwh/3683045 to your computer and use it in GitHub Desktop.
Selecting enabling for operators in Breeze... Enable
This file contains 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
trait Enable[T] { | |
def get: T | |
} | |
trait EnableLowPriority { | |
implicit val eager = new Enable[Eager]{def get = new Eager} | |
} | |
object Enable extends EnableLowPriority { | |
object Lazy { | |
implicit val lzy = new Enable[Lazy]{def get = new Lazy} | |
} | |
} | |
object Disable { | |
object Eager { | |
// I think I'm cute. | |
implicit val `LISTEN: you disabled eager evaluation, but no lazy implicit was found` = new Enable[Eager]{def get = new Eager} | |
implicit val `-- Navi` = new Enable[Eager]{def get = new Eager} | |
} | |
} | |
case class Eager() { | |
def dance() = () | |
} | |
case class Lazy() { | |
def sleep() = () | |
} | |
object X { | |
def foo[R](x: Int, b: Int)(implicit enable: Enable[R]): R = enable.get | |
def xxx { | |
foo(3,4).dance() | |
//foo(3,4).sleep() // error! | |
} | |
def zzz { | |
import Enable.Lazy._ | |
foo(3,4).sleep() | |
//foo(3,4).dance() // error! | |
} | |
class Baz[R] | |
implicit val eagerBaz = new Baz[Eager] | |
def bar[R](x: Int, b: Int)(implicit baz: Baz[R], enable: Enable[R]):R = enable.get | |
def canBeEagerEvenWithLazy(x: Int, b: Int) = { | |
import Enable.Lazy._ | |
bar(3,4).dance() | |
} | |
def canDisableEagerEvaluation { | |
import Enable.Lazy._ | |
import Disable.Eager._ | |
// bar(3,4).dance() // error! | |
/* | |
foo.scala:58: error: ambiguous implicit values: | |
both value LISTEN: you disabled eager evaluation, but no lazy implicit was found in object Eager of type => java.lang.Object with Enable[Eager] | |
and value -- Navi in object Eager of type => java.lang.Object with Enable[Eager] | |
match expected type Enable[Eager] | |
bar(3,4).dance() // error! | |
*/ | |
() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment