11

I have started learning hacklang today and now I am a bit stuck on shapes: http://docs.hhvm.com/manual/en/hack.shapes.php

I understand the concept of shapes and it seems really useful for me, but I can't understand why for example this code does not throw any error:

<?hh

type Point2D = shape('x' => int, 'y' => int);

function dotProduct(Point2D $a, Point2D $b): int {
    return $a['x'] * $b['x'] + $a['y'] * $b['y'];
}

function main_sse(): void {
    echo dotProduct(shape('x' => 3, 'y' => 'this should cause an fatal error?'), shape('x' => 4, 'y' => 4));
}

main_sse();

The 'y' key is defined as integer, but when I pass a string, no error is shown. Thanks for your help :)

1
  • 1
    You also want "<?hh //strict" at the top of your file on new files, if at all possible.
    – Dean J
    Jun 25, 2017 at 21:13

2 Answers 2

14

Actually executing Hack code doesn't necessarily typecheck everything. You need to actually run a separate tool to enforce the type system, as described in the docs article linked here. When you do that, you'll get an error that looks something like this, depending on the exact version of HHVM you have:

File "shapes.php", line 10, characters 19-23:
Invalid argument (Typing[4110])
File "shapes.php", line 3, characters 41-43:
This is an int
File "shapes.php", line 10, characters 42-76:
It is incompatible with a string

Modern versions of HHVM will also yell at you if you aren't running the typechecker; I suspect you're running an older version, before we realized this was a point of confusion -- sorry!

What actually happens when you run type-incorrect code is undefined behavior. The answer by Ed Cottrell is correct for the current version of HHVM -- we do the same thing PHP does to coerce types -- but keep in mind it's undefined behavior and may change in future versions without notice.

1
  • Thanks for the answer :) Well currently I am using HipHop VM 3.3.0-dev+2014.08.22 (rel). To my knowledge it is the newest public version. Anyway, hope that HHVM will throw an exception or an error for this kind of type mismatch in the future :)
    – WebDevHere
    Sep 19, 2014 at 5:54
3

The interpreter will try to evaluate the 'y' key as a number to do the calculation.

Example:

echo 4 * '6';
// prints 24

echo 4 * '6foo';
// prints 24

echo 'foo' * 42;
// prints 0, because floatval('foo') === 0

Your situation is like the third example. floatval('this should cause an fatal error?') === 0, so the calculation is:

$a['x'] * $b['x'] + $a['y'] * $b['y'] === 3 * 4 + 0 * 4 === 12
3
  • Thanks for your answer :) but what is the point of the shapes then? what is the point of defining that 'y' is integer? It works just like regular array.
    – WebDevHere
    Sep 18, 2014 at 14:55
  • I'm a little out of my depth here, having never played with hacklang. But clearly the interpreter is doing a conversion. I think the problem is that you are looking at the type definition for Point2D, but your dotProduct call uses shape, not Point2D, so the type check never occurs.
    – elixenide
    Sep 18, 2014 at 14:59
  • 1
    This is indeed a type error, you just need to actually look for type errors separately -- please see my answer for a full explanation. Sep 18, 2014 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.