Last active
August 29, 2015 14:25
-
-
Save Jentsch/d77193839ba83e07e8f5 to your computer and use it in GitHub Desktop.
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
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