Created
June 23, 2012 15:52
-
-
Save Centaur/2978757 to your computer and use it in GitHub Desktop.
Indifferent Map for scala
This file contains hidden or 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 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