Created
May 30, 2019 11:18
-
-
Save JoolsF/9ec7b05f2604ca483272a13aff53a938 to your computer and use it in GitHub Desktop.
Type constraints example 1
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
/* | |
* Problem - Constraining a parameter to only take a specific type, not its subtype | |
* | |
* https://herringtondarkholme.github.io/2014/09/30/scala-operator/ | |
*/ | |
class Foo() | |
class Bar() extends Foo | |
def onlyTakesFoo[A](f: A)(implicit ev: A =:= Foo) = "Foo" | |
def onlyTakesBar[A](f: A)(implicit ev: A =:= Bar) = "Bar" | |
onlyTakesFoo(new Foo()) | |
onlyTakesFoo(new Bar) //won't compile | |
onlyTakesBar(new Bar) | |
onlyTakesBar(new Foo) //won't compile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment