Implement typesafe substitution in Scala