Hackle Forms

I started learning hacklang today, and now I'm a bit stuck in forms: http://docs.hhvm.com/manual/en/hack.shapes.php

I understand the concept of shapes and it seems really useful to me, but I can’t understand why, for example, this code does not cause 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 key 'y' is defined as an integer, but when I pass the string, the error is not displayed. Thanks for the help:)

+7
hhvm hacklang
source share
2 answers

Actually executing the hack code is not necessarily typecheck all. In fact, you need to run a separate tool for securing the type system, as described in the documentation article provided here . When you do this, you will get an error that looks something like this, depending on the specific version of HHVM that 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 shout at you if you are not using typechecker; I suspect you are using an older version before we realized that it was a confusion - sorry!

What actually happens when you run the wrong code like undefined. Ed Cottrell's answer is correct for the current version of HHVM - we do the same thing as PHP to force types, but keep in mind that this is undefined behavior and may change in future versions without notice.

+9
source share

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

Example:

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

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

 $a['x'] * $b['x'] + $a['y'] * $b['y'] === 3 * 4 + 0 * 4 === 12 
+2
source share

All Articles