Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Created May 30, 2019 11:18
Show Gist options
  • Save JoolsF/9ec7b05f2604ca483272a13aff53a938 to your computer and use it in GitHub Desktop.
Save JoolsF/9ec7b05f2604ca483272a13aff53a938 to your computer and use it in GitHub Desktop.
Type constraints example 1
/*
* 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