> -----Ursprüngliche Nachricht----- > Von: Pavel Kouřil [mailto:pajou...@gmail.com] > Gesendet: Sonntag, 22. Februar 2015 22:18 > An: Robert Stoll > Cc: Zeev Suraski; PHP internals > Betreff: Re: [PHP-DEV] Coercive Scalar Type Hints RFC > > On Sun, Feb 22, 2015 at 9:42 PM, Robert Stoll <p...@tutteli.ch> wrote: > > > > 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 > > > > Well, > > I look at it this way (in a simplified manner). Hopefully this will make you > understand my point of view more. > > - Implicit conversions work only when you are sure you won't lose stuff > - Explicit conversions are for forcing (casting) variable to become another > type, and when you are explicitely as user calling > it, you are aware you can lose values >
I see. I see and think you are not alone with this opinion. I give you another example and hope you reconsider your position (up to you what position you take afterwards of course). Consider the following in C# class A{} class B : A{} class C : A{} A a = new B(); B b = a; // will fail, needs a conversion C c1 = a; // will fail, needs a conversion C c2 = (C) a; //will fail at runtime And now imagine C# would not be based on types but on values. Then the following would be perfectly legal as well: B b = a; //is fine since a is of type B C c1 = c; //will fails since a is not of type C C c2 = (C) c; //still fails since a is not of type C Or to illustrate it differently. Imagine you have a shop and your main currency is $. However, you accept € as well as long as they are banknotes. In this case the customer can insert the banknotes in a currency exchange machine at the till. Now imagine the following four use cases: 1. A customer buys something with $ -> everything is fine because that is your main currency. 2. Another customer buys in € and pays in banknotes, also no problem at all. 3. A third customer wants to pay in € but with coins. Your implicit rule does not hold and you reject the customer. 4. Another customer comes by and mentions explicitly that she wants to pay in € with coins. Suddenly you do not care about it any longer and try to squeeze the coins into your machine. Unfortunately it does not work at all and what you get out of the machine is nothing. To me, that is exactly how PHP behaves today. Just take int instead of $ and string instead of €, banknotes are ints wrapped in string (e.g. "1") and coins are "a", "b" etc. I really do not think it is a clever idea to have such a big difference between implicit and explicit conversion. They should behave the same way. I do not care if you rename explicit casts to something different (I proposed oldSchoolIntConversion, but maybe forceIntConversion would be good enough as well). > Sure, the literal meaning in C# and PHP differs a little bit (because of > static and dynamic typed language differences and > stuff), but the > *intent* is IMHO the same; implicit conversions can happen in the > "background" safely, while for "dangerous" conversions, > you have to cast by hand. And I see use cases for both of these types of > conversions. C# also has the "as" syntax which is another form of conversion. But it is another form of conversion, uses another syntax and hence can have other rules. PHP could also have different conversion rules but they should be named differently. Using implicit/explicit and having a different behaviour is inconsistent IMO (ok, I said it too often in this mail, I'll stop now) > > Also, you are assuming that there will be a "strict" mode; I sincerely hope > there won't. Ssince introduction of "2 modes", I > was always saying that there should be only one mode - I don't really care > whether it would be strict or weak, but just only > one. > I do not assume that there will be one but I leave the door open rather than close it (unnecessarily). > Regards > Pavel Kouril > > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: > http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php