Created
June 29, 2017 10:16
-
-
Save gigiigig/576910458bbf28be9d281d0c4bd9525f to your computer and use it in GitHub Desktop.
Change Return type of a function
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 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