Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Created June 29, 2017 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigiigig/576910458bbf28be9d281d0c4bd9525f to your computer and use it in GitHub Desktop.
Save gigiigig/576910458bbf28be9d281d0c4bd9525f to your computer and use it in GitHub Desktop.
Change Return type of a function
{
trait Aux[T] {
type R
def apply(t: T): R
}
implicit val sa = new Aux[String] {
type R = Boolean
def apply(s: String): Boolean = s.isEmpty
}
implicit val ia = new Aux[Int] {
type R = String
def apply(i: Int): String = i.toString
}
def foo[T](t: T)(implicit aux: Aux[T]): aux.R = aux(t)
}
/*
@ val b: Boolean = foo("")
b: Boolean = true
@ val c: String = foo(2)
c: String = "2"
@ val c: Boolean = foo(4)
cmd4.sc:1: type mismatch;
found : ammonite.$sess.cmd0.ia.R
(which expands to) String
required: Boolean
val c: Boolean = foo(4)
^
Compilation Failed
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment