> -----Ursprüngliche Nachricht-----
> Von: Pavel Kouřil [mailto:pajou...@gmail.com]
> Gesendet: Sonntag, 22. Februar 2015 20:02
> An: Robert Stoll
> Cc: Zeev Suraski; PHP internals
> Betreff: Re: [PHP-DEV] Coercive Scalar Type Hints RFC
> 
> On Sun, Feb 22, 2015 at 7:30 PM, Robert Stoll <p...@tutteli.ch> wrote:
> > Hi Pavel,
> >
> > Yes, I am suggesting to make conversions behave the same regardless if it 
> > is implicit or explicit. The only difference
> between the two should be that one is stated explicitly by the user where the 
> other is applied implicitly. Other
> programming languages behave like this and are more predictable for users as 
> well as developers because one does not
> need to learn two sets of conversion rules.
> >
> 
> Actually this is not true. Other languages have differences between explicit 
> conversions (aka casting) and implicit
> conversions as well.
> C# is the language I use the most after PHP, so I'll bring that one up (see 
> https://msdn.microsoft.com/en-
> us/library/ms173105.aspx), but I believe other languages (probably Java?) act 
> the same way.
> 
> Regards
> Pavel Kouril
> 

Hm... I reconsidered my statements and that is a good thing :)
I am not sure if I got your view point. I will try to elaborate more on mine 
and explain how I interpret your statement.

Probably it is a philosophical question how to look at it. IMO the only 
difference in C# (as well as in Java) lies in the way the conversions are 
applied. Implicit conversions are applied automatically by the compiler where 
explicit conversions are applied by the user. The difference lies in the fact 
that C# is statically typed and implicit conversions are only applied when it 
is certainly safe to apply one. However, Implicit conversions in C# behave the 
same as explicit conversion since implicit conversion which fail simply do not 
exist (there is no implicit conversion from double to int for instance). That 
is the way I look at it. You probably look at it from another point of view and 
would claim an implicit conversion from double to int in C# exists but just 
fails all the time => ergo implicit and explicit are different (that is my 
interpretation of your statement above). In this sense I would agree. But even 
when you think in this terms then you have to admit, they are fundamentally 
different in the way that implicit conversion which are different than explicit 
conversion always fail, in all cases - pretty much as if they do not exist. 
There are no cases, neither in C# nor in Java which I am aware of, where an 
implicit cast succeeds in certain cases but not in all and an explicit 
conversion succeeds in at least more cases than the implicit conversion. Hence, 
something like "a" should also not work in an explicit conversion in PHP IMO if 
it is not supported by the implicit conversion (otherwise strict mode is 
useless btw.)

Try out the following C# code:
dynamic d1 = 1.0;
int d = d1;
You will get the error "Cannot implicitly convert type `double` to `int`" at 
runtime.

We see a fundamental difference between C# and PHP here. PHP is dynamically 
typed an relies on values rather than types (in contrast to C#). Therefore, the 
above code emits a runtime error even though the data could be converted to int 
without precision loss.
This shall be different in PHP according to this RFC and I think that is 
perfectly fine. Yet, even more important it seems to me that implicit/explicit 
conversions behave the same way. 
At first it might seem strange to have just one conversion rule set in PHP 
since PHP is not known to be a language which shines due to its consistency... 
OK, I am serious again. If you think about it from the following point of view: 
A user writes an explicit conversion in order to state explicitly that some 
value will be converted (this is something which will be necessary in a strict 
mode). Why should this explicit conversion be different from the implicit one? 
There should not be any difference between explicit knowledge and implicit one. 
That is my opinion. If you really do not care about data loss and just want to 
squeeze a float/string into an int no matter what the value really is then you 
can use the @ in conjunction with ?? and provide the desired default value to 
fall back on if the conversion fails. If conversions like "a" to int really 
matters that much to the users of PHP then we could keep the 
oldSchoolIntConversion function (as propose in my first email) even in PHP 10 
(I would probably get rid of them at some point).

Cheers, 
Robert


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

Reply via email to