On Thu, Feb 12, 2015 at 3:31 PM, Andrea Faulds <a...@ajf.me> wrote:
> Hi Pavel,
>
>> On 12 Feb 2015, at 13:48, Pavel Kouřil <pajou...@gmail.com> wrote:
>>
>> C# does have dynamic typing.
>
> No it doesn’t, it’s a statically-typed language. I don’t understand why you 
> say it has dynamic typing - there is some limited dynamism in parts, but I 
> don’t think it affects what we’re talking about. Dynamic typing and 
> polymorphism aren’t the same.
>

I never said dynamic typing and polymorphism are the same, or did I?
Also, C# has type "dynamic", which allows for usage of dynamic typing
in C# (technically, under the hood, it is an System.Object type
without the need for casting, IIRC).

>> I just don't believe that method overloading would suddenly make
>> everyone write poor programs.
>
> Maybe not, but all the facilities needed to make good programs are already 
> there. Overloading would only really be useful for writing bad programs, 
> because all the good things it enables are already supported by PHP.
>
>> It brings cleaner declarations of methods and functions.
>
> Only slightly. A switch() (or better, a pattern match, were we to add that) 
> can do the same job and it’s not that much less clean.
>

Actually, I wouldn't mind having both pattern matching and method
overloading in PHP. I'd say that both of them have their use, and not
everywhere you'd use pattern matching you'd use method overloading and
vice versa.

function foo(Foo $a) {}
function foo(Foo $a, Bar $b) {}
function foo(Foo $a, Baz $c) {}

This is much much more cleaner than doing:

/**
 * description of what it does depending if you pass Bar or Baz or null
 */
function foo(Foo $a, $b = null) {}

In pattern matching it could be done with, let's say:
foo $a, $b | $b instanceof Bar = {}
                 | $c instanceof Baz = {}

Which is IMHO as unclean as using ifs statements inside the method. :(

And at the same time, I don't see how you could do this via method overloading:

signum x |  x < 0  = -1
               |  x > 1 = 1
               |  x == 0 = 0

I hope this example is correct, it's been a while since I used Haskell.

>> Also, it
>> brings the possibility of having operator overloading in user classes
>> someday in the future (yes, this can be abused as well, but it also
>> has legitimate use cases - like class for representing Money and being
>> able to do +, - , / or * operations on them).
>
> Overloading isn’t necessary for operator overloading.
>

It isn't, but both would be nice.

>> Is the option of omiting
>> type hints and having optional parameters really a good enough
>> replacement for method overloading polymorphism for you? Because for
>> me, it unfortunately is not. :(
>
> Well, yes. PHP is dynamically-typed, this can happen, whether you’re using 
> strict or weak typing. Your only way to guard against it here is to do proper 
> testing. Well, if we added a numeric type hint, your IDE could catch this 
> error for you, actually.
>
> Anyway, the same thing can happen for float->int conversions *with* weak 
> typing, as we disallow certain conversions (PHP_INT_MIN > $x,  PHP_INT_MAX < 
> $x, !isfinite($x), isnan($x)).
>
> Also, even though PHP does allow most float->int conversions with weak 
> typing, it doesn’t mean they’re a good idea. Silently losing the fractional 
> part is probably not what you want.
>

As I stated few times already, I find float -> int a bad idea (even in
weak mode). That's at least something we can agree on I guess? :)


Regards
Pavel Kouril

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

Reply via email to