RE: [PHP-DEV] Return Type Hinting for Methods RFC

From: Date: Thu, 22 Dec 2011 19:49:03 +0000
Subject: RE: [PHP-DEV] Return Type Hinting for Methods RFC
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  Groups: php.internals 
Request: Send a blank email to internals+get-57025@lists.php.net to get a copy of this message
From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com] > How is that hard? > > function age_lookup($age) { > return $coll->find(array('age' => (int)$age); > } > > but again here, doing a strong type check on the parameter isn't making your life easier. > It simply pushes the responsibility to the caller and introduces a tricky unrecoverable error that > will drive you crazy unless you have 100% regression test coverage (which is kind of impossible > since the number of inputs is infinite) or great static analysis tools. PHP is not a compiled language, so you end up not catching these until runtime which is obviously sub-optimal. > -Rasmus This will silently fail in a very bad way when the caller accidentally passes in (for example) an array. With a scalar type hint it would die loudly (quickly alerting the developer to the problem) and code analysis tools (even just a decent IDE) could highlight the error even before it is executed. The point here is that the caller is already responsible to make sure it passes the right value, but the language currently offers no means of documenting that, and no easy way to ensure it. There's obviously a question about how much juggling to allow with a scalar type hint. IMO the obvious stuff ("36" to 36, etc.) is perfect, but passing "foobar" or array(1,2,3) to an int should throw an error. John Crenshaw Priacta, Inc.

« previous php.internals (#57025) next »