AW: [PHP-DEV] [VOTE] Scalar Type Hints

From: Date: Thu, 12 Feb 2015 17:22:33 +0000
Subject: AW: [PHP-DEV] [VOTE] Scalar Type Hints
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  Groups: php.internals 
Request: Send a blank email to internals+get-82548@lists.php.net to get a copy of this message
Hi François, > -----Ursprüngliche Nachricht----- > Von: François Laupretre [mailto:francois@tekwire.net] > Gesendet: Donnerstag, 12. Februar 2015 18:01 > An: 'Robert Stoll'; 'Nikita Nefedov'; 'Andrea Faulds' > Cc: 'Pavel Kouřil'; 'PHP Internals' > Betreff: RE: [PHP-DEV] [VOTE] Scalar Type Hints > > Hi Robert, > > > De : Robert Stoll [mailto:php@tutteli.ch] > > > > There are several programming languages which do support dynamic > > typing and method overloading somehow (Clojure, Erlang, Prolog, Cecil > > and certainly more). Most of them use a multi-method approach and I > > think if PHP would introduce function/method overloading, then such an > > approach would be appropriate. Right now, I need to implement the > > dynamic dispatching quasi myself which might be more verbose but is > > also more cumbersome (and uglier IMO). Consider the following (fictional example): > > We all agree on the interest of polymorphism on object classes, even if your example could be > solved with specialized > logger classes, but the question was about polymorphism on different scalar types, which is > much more problematic in PHP, > even using strict scalar typing. > > Cheers > > François OK, maybe I went a little bit off topic due to the booted discussion about method overloading which did not cover all aspects IMO. So back to scalar type hints: I do not really see how num would be a problem or polymorphism in general. I am not really aware of memory management of PHP but I guess it is not optimised for fixed-sized integers or is it? If not, then it does not really matter whether a parameter holds an int, a float or a BigInt, allocation is dynamic anyway I suppose. Or what problems do you see here? Maybe you are referring to Pavel's example: > the issue I see with it is that bar() requires float, but foo() just number and doesn't > convert it to the required type. The most unpredictable thing is that it will work half of the time > and half of the time it will just throw errors The same applies for the following code class Foo{} class Bar{} function foo(Foo foo){ bar($foo); } function bar(Bar bar){ //.. } and we still agree upon that polymorphism is the right way to go since it allows more expressive code with a certain "safety". If you want full flexibility, then rely on dynamic typing only and if you want some safety then use type hints. I do not see a special case here - polymorphism for scalar types is nothing new

« previous php.internals (#82548) next »