Google News
logo
Hack - Interview Questions
Explain Expression Trees in Hack.
Expression trees are an experimental Hack feature that allow you write domain specific languages (DSLs) with Hack syntax.

You can write expressions using normal Hack syntax, and all the normal Hack tools work: syntax highlighting, type checking, and code completion.

Unlike normal Hack code, expression tree code is not executed.
$e = MyDsl`foo() + 1`;​

What is executed at runtime?
Hack converts an expression tree to a series of method calls on a visitor.
// You write code like this:
$e = MyDsl`1 + 2`;

// Hack runs the following "desugared" code:
$e =
  MyDsl::makeTree<MyDslInt>(
    ($v) ==> $v->visitBinop(
      $v->visitInt(1),
      '__plus'
      $v->visitInt(2))
  );​

The visitor can also define the types associated with literals and operators. This enables Hack to typecheck the DSL expression, even if 1 doesn't represent a Hack int.
// The type checker will also check that this "virtualized" expression is correct,
// even though this virtualized code isn't executed.
(MyDsl::intType())->__plus(MyDsl::intType());​
Advertisement