Skip to content

Instantly share code, notes, and snippets.

@Jentsch
Last active August 29, 2015 14:25
Show Gist options
  • Save Jentsch/d77193839ba83e07e8f5 to your computer and use it in GitHub Desktop.
Save Jentsch/d77193839ba83e07e8f5 to your computer and use it in GitHub Desktop.
class Key[V] {
type Value = V
def ->(value: V) = Pair(this, value)
}
trait Pair {
val key: Key[_]
val value: key.Value
}
trait Map {
val pairs: Seq[Pair]
def get[V](key: Key[V]): Option[V] =
pairs.find(_.key eq key).map{_.value.asInstanceOf[V]}
}
object Pair {
def apply[V](key2: Key[V], value2: V): Pair = new Pair {
val key: Key[V] = key2
val value = value2
}
}
val text = new Key[String]
val count = new Key[Int]
val map: Map = new Map {
val pairs = text -> "Hello World!" :: Nil
}
map.get(text) // Some(Hello World!), Type: Option[String]
map.get(count) // None, Type: Option[Int]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment