hacklang

How to instantiate a shape type object in hacklang


This seems like it should be in the documentation, but it isn't.

I have a shape type.

type ThisIsMyShapeType = shape(
  'some_prop' => bool,
);

How do I instantiate an object of this type ?


Solution

  • You use the same shape keyword:

    $x = shape(
      'some_prop' => true,
    );
    

    In Hack, shapes use structural typing so you don't need to declare your variable is a ThisIsMyShapeType -- the typechecker will verify that $x has all of the right fields to match the type.

    There are quite a few examples in the official documentation.