Skip to content

Instantly share code, notes, and snippets.

@propensive
Created January 25, 2015 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save propensive/97f70ce67bdb38022c47 to your computer and use it in GitHub Desktop.
Save propensive/97f70ce67bdb38022c47 to your computer and use it in GitHub Desktop.
"Proxies" with implicits and singleton types
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