Created
January 25, 2015 10:51
-
-
Save propensive/97f70ce67bdb38022c47 to your computer and use it in GitHub Desktop.
"Proxies" with implicits and singleton types
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
package proxy | |
import language.dynamics | |
abstract class Param[T] { type Type } | |
object Param { | |
def apply[T](name: String) = new Param[name.type] { type Type = T } | |
} | |
class Proxy(underlying: Map[String, Any]) extends Dynamic { | |
def selectDynamic[T](name: String)(implicit param: Param[name.type]): param.Type = | |
underlying(name).asInstanceOf[param.Type] | |
} | |
object Test { | |
val proxy = new Proxy(Map("foo" -> "Hello world", "bar" -> 42, "baz" -> 'xyz)) | |
implicit def foo = Param[String]("foo") | |
implicit def bar = Param[Int]("bar") | |
implicit def baz = Param[Symbol]("baz") | |
println(proxy.foo) | |
println(proxy.bar) | |
println(proxy.baz) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment