Skip to content

Instantly share code, notes, and snippets.

@okram
Created June 18, 2020 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okram/e8d47df897a10a69db98e90172d4153e to your computer and use it in GitHub Desktop.
Save okram/e8d47df897a10a69db98e90172d4153e to your computer and use it in GitHub Desktop.
@scala.annotation.tailrec
def fetchOption[A <: Obj](obj: Obj, label: String): Option[A] = {
obj match {
case x if x.root => None
case x if x.via._2.op == Tokens.to && x.via._2.arg0[StrValue].g == label => obj match {
case _: Value[Obj] => Some(x.via._1.asInstanceOf[A])
case _: Type[Obj] => Some(x.via._1.range.from(label).asInstanceOf[A])
}
case x if x.via._2.op == Tokens.rewrite && x.via._2.arg0[Obj].name == label => Some(Inst.resolveArg(obj, x.via._2.arg0[A]))
case x if x.via._2.op == Tokens.define && x.via._2.arg0[Obj].name == label => Some(Inst.resolveArg(obj, x.via._2.arg0[A].named(baseName(x.via._2.arg0[A]))))
case x => fetchOption(x.via._1, label)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment