2
shape x {
   ?'a' => ?string,
   ?'b' => ?string,

}

I have an array of shape x, And I am trying to see if any of the shapes have a value of 'hello' in field 'a'. How can i do so using built in functions?

  • Shapes act exactly like arrays from the reading side, except values are typed, and they require a string literal to index them. Are you having a specific difficulty with that shape? – concat Jun 26 '20 at 01:29

1 Answers1

1
function has_hello(vec<shape(?'a' => string, ?'b' => string)> $items): bool {
  foreach ($items as $item) {
    if (Shapes::idx($item, 'a') === 'hello') {
      return true;
    }
  }
  return false;
}

You can use Shapes::idx to get the value of an optional field. If you're looking for helper functions to iterate on arrays, check out the C\ namespace.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192