xp-framework/rfc

Hack language support

thekid opened this issue · 1 comments

Scope of Change

The XP Framework and libraries will support Hack language features.

Rationale

With XP and most of its libraries completely working on HHVM, Facebook's continued investment, production usecases surfacing, and HHVM for Windows around the corner, Hack language is available in a more widespread manner.

Functionality

The functionality is broken down into the following:

✅ Consistent behavior

In XP 7.0.0, the framework will add basic support for the following:

  • Running hack code alongside PHP - seamless
  • The short hack lambdas inside regular code - seamless
  • Hack parameter and return types, including void, this, noreturn, the num and arraykey unions, function types and typed arrays array<T> and maps array<?, T> - via changes to type system
  • Hack enums to be recognized as enum types correctly - by checking for HH\BuiltinEnum

Nullable types and soft type hints are displayed but ignored otherwise.

✅ Annotations

The mirrors library supports Hack attributes since version 0.4.0, and lets you access them via a common interface with XP's annotations:

<?hh

<<example(['author' => 'thekid'])>>
class Test {
}
$ XP_RT=hhvm xp -m ../mirrors/ -w '(new \lang\mirrors\TypeMirror("Test"))->annotations()'
lang.mirrors.Annotations([
  example => lang.mirrors.parse.Value<[
    author => "thekid"
  ]>
])

Shapes and tuples

Shapes and Tuples should find their place in the type system.

type Point = shape('x' => int, 'y' => int);
$this->origin = shape('x' => $x, 'y' => $y);

tuple(value1, ..., value n); // for creation
(type1, ..., type n); // for annotation.

Async

Asynchronous methods should be supported via mirrors.

Generics

Hack generics should find a place in the typesystem, as should type constants

<?hh
// A generic class
class List<T> {
  public function add(T $element): self { ... }
}

// Type constants
abstract class List {
  abstract const type T;
  public function add(this::T $element): self { ... }
}

class StringList extends List {
  const type T = string;
}

Security considerations

n/a

Speed impact

Slight performance reduction due to additional checks on type names and special handling inside reflective access to types - in reality, not really measurable.

Dependencies

XP must be running on HHVM, obviously.

Related documents

First two steps implemented