Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dependent Types
trait DependentValue{
type V
val value: V
}
//object DependentValue {
// def apply[A](a: A): DependentValue { type V = A } = new DependentValue {
// type V = A
// val value = a
// }
//}
object DependentValue {
trait Aux[A] extends DependentValue { type V = A }
def apply[A](a: A): DependentValue.Aux[A] = new Aux[A] {
val value = a
}
}
def magic(input: DependentValue): DependentValue = input.value
val dep1: DependentValue.Aux[Int] = DependentValue(1)
val depTwo = DependentValue("two")
val intReturnValue: Int = magic(dep1)
val stringReturnValue: String = magic(depTwo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment