Lars Strojny wrote (on 26/09/2014):
Hi everyone,

On 24 Sep 2014, at 23:17, Rowan Collins <rowan.coll...@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.

I'm not sure how you see these as equivalent. As I mentioned, a switch statement is a glorified if/elseif/else, or when fallthrough is used, a glorified set of if-then-gotos.

fact(N) when N>0 ->
     N * fact(N-1);

fact(0) ->
     1.

Without the additional guarantees provided by a purely functional environment, that's really just inverting the function header and if statement.

Adding an extra case to that still means repeating the operator, so isn't the same as what I was talking about at all:

foo(N) when N>100 ->
    N ** foo(N-10);

foo(N) when N>0 ->
    N * foo(N-1);

foo(0) ->
    1.


In Scala one could replicate the instanceof behaviour by defining multiple 
methods of the same name with different argument types.

Or, indeed, in most strongly typed languages; in OOP, polymorphism can be exploited for the same purpose, e.g. using the Visitor Pattern. Again, I'm not sure what this has to do with switch statements, except that overloading and polymorphism can both allow you to factor out *any* if/switch statement, if you so desire.

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to