Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created June 23, 2012 15:52
Show Gist options
  • Save Centaur/2978757 to your computer and use it in GitHub Desktop.
Save Centaur/2978757 to your computer and use it in GitHub Desktop.
Indifferent Map for scala
class IndifferentMap[K >: String with Symbol ,V](original:Map[K,V]) extends collection.MapProxy[String, V] {
val beafed = original.map{
case (k:Symbol,v) => k.name->v
case (k:String, v) => k->v
}
override def self = beafed
def apply(s:Symbol):V = apply(s.name)
}
val m = new IndifferentMap(Map("abc"->1))
val m2 = new IndifferentMap(Map('abc->1))
assert(m('abc) == 1)
assert(m("abc") == 1)
assert(m2('abc) == 1)
assert(m2("abc") == 1)
val m3 = new IndifferentMap(Map(1 -> 2)) // compile error
val m4 = new IndifferentMap(Map(true -> 2)) // compile error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment