Re: Little switch improvement: get the used variable
| From: | Lars Strojny | Date: | Fri, 26 Sep 2014 08:23:21 +0000 |
| Subject: | Re: Little switch improvement: get the used variable | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77654@lists.php.net to get a copy of this message | ||
Hi everyone,
On 24 Sep 2014, at 23:17, Rowan Collins <rowan.collins@gmail.com> wrote:
> switch ( $number ) use ( === ) {
[...]
> switch ( $age ) use ( < ) {
[...]
> switch ( calculate_age($birth_date, $departure_date) as $age_at_departure ) use ( < ) {
[...]
> switch ( $product ) use ( instanceOf ) {
All of these examples are trying to reinvent concepts that can be solved with guards, pattern
matching and overloading in other languages. In Erlang one can write the same function name multiple
times and guard it ("when" in fact(N)) or match the value ("0" in fact(0)).
fact(N) when N>0 ->
N * fact(N-1);
fact(0) ->
1.
In Scala one could replicate the instanceof behaviour by defining multiple methods of the same name
with different argument types.
So I would rather see us making method declarations more flexible to cover those use cases instead
of shoving all the functionality into switch/case. Last time I checked the interpreter to try and
introduce more features around method declarations, it looks incredibly hard to do in a way that
performs well. What is our stance on adding advanced features around declaring methods?
cu,
Lars
Attachment: [application/pgp-signature] Message signed with OpenPGP using GPGMail signature.asc
Attachment: [application/pgp-signature] Message signed with OpenPGP using GPGMail signature.asc