It would be best to avoid loading code in at runtime.
Here is an example solution using Scala's 2.11's JSR-223 support:
scala> import javax.script._
import javax.script._
scala> val e = new ScriptEngineManager().getEngineByName("scala");
e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@26969ee5
scala> val pf = e.eval("""{case <red>{scala.xml.Text(txt)}</red> => txt }: PartialFunction[scala.xml.Elem, String]""").asInstanceOf[PartialFunction[scala.xml.Elem, String]]
pf: PartialFunction[scala.xml.Elem,String] = <function1>
scala> pf(<yellow/>)
scala.MatchError: <yellow/> (of class scala.xml.Elem)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246)
at $anonfun$1.applyOrElse(<console>:8)
at $anonfun$1.applyOrElse(<console>:8)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
... 32 elided
scala> pf(<red>RED</red>)
: String = RED
You can convert the pf
into an extractor if you like. Relevant reading: