Skip to content

Instantly share code, notes, and snippets.

@szoio
Created June 9, 2020 04:50
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 szoio/23caa238b03929750c28865728f6524e to your computer and use it in GitHub Desktop.
Save szoio/23caa238b03929750c28865728f6524e to your computer and use it in GitHub Desktop.
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