hacklang - Catchable fatal error: Hack type error -


example presentation on page 31

class foo<t>  {      public function add(t $delta): foo {         $this->num += $delta;        // line 6         return $this;     }      public function get(): t {          return $this->num;     }      public function __construct(private t $num): void {} }  $f1 = new foo(123); $f1->add(567); echo $f1->get(), php_eol;  $f2 = new foo(1.23); echo $f2->add(5.67)->get(), php_eol; 

error

catchable fatal error: hack type error: typing error @ example.php line 6

what problem?


hiphop vm 3.11.1 (rel)

compiler: tags/hhvm-3.11.1-0-g64d37362bc0b6aee919492ad61cf65ce0a5d5e92

repo schema: 8b80ba45250a6669cd610c189dbbb55b6218c2a3

if run typechecker (hh_client), you'll error like:

this num because used in arithmetic operation. incompatible value of generic type t

this because + operator requires both sides num types, t can type.

you can add constraint t has num (class foo<t num>), or use num type instead of generic t.

using num allow mix floats , ints in same instance. using constraint, instance work either floats or ints, not both.


Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -