Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83487 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51647 invoked from network); 22 Feb 2015 21:17:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Feb 2015 21:17:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=pajousek@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=pajousek@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.48 as permitted sender) X-PHP-List-Original-Sender: pajousek@gmail.com X-Host-Fingerprint: 209.85.216.48 mail-qa0-f48.google.com Received: from [209.85.216.48] ([209.85.216.48:34582] helo=mail-qa0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E3/85-18531-C774AE45 for ; Sun, 22 Feb 2015 16:17:51 -0500 Received: by mail-qa0-f48.google.com with SMTP id dc16so19618357qab.7 for ; Sun, 22 Feb 2015 13:17:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=abW6QGmvarXbFoffF6F1VLUr/woW5I6iDhE7EoVzM9M=; b=MXxCGC+dJduwtNGvvezlfj8UsAiijhV5W+t4TX5Ar5Oy9IxmgFdlVtExtHmx1+8bcC ibcWL3icX+U4w+4jMJYcilUPMlE71y2rfu/nPbP59QK2bbCqXz7+L7iXqWvtfILHhKm4 KOsKk/vBTZb3EvBQWlp2axUnX+KQSW8lW3OOHyw1M9d+3Y8mFWIqTGQ3FN9xy86goemi 1mbkmGCCunmcMOGPM+9QIQD693QW77+aMCC6YiqGIdwNd4T/FK/cLs8sZ0a8KhEACEDz kBnuyZRVucWx0DNlx964ECArybs9a7EtHg5t7bmca5MFs9sleH6pz8gMCpMwnTJnG6NN 4e0w== MIME-Version: 1.0 X-Received: by 10.140.234.198 with SMTP id f189mr19252413qhc.0.1424639866699; Sun, 22 Feb 2015 13:17:46 -0800 (PST) Received: by 10.96.160.99 with HTTP; Sun, 22 Feb 2015 13:17:46 -0800 (PST) In-Reply-To: <000d01d04ee0$0d03fb70$270bf250$@tutteli.ch> References: <7ef509ef10bb345c792f9d259c7a3fbb@mail.gmail.com> <002101d04ea0$d92ea0f0$8b8be2d0$@tutteli.ch> <000601d04ecd$95545aa0$bffd0fe0$@tutteli.ch> <000d01d04ee0$0d03fb70$270bf250$@tutteli.ch> Date: Sun, 22 Feb 2015 22:17:46 +0100 Message-ID: To: Robert Stoll Cc: Zeev Suraski , PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Coercive Scalar Type Hints RFC From: pajousek@gmail.com (=?UTF-8?Q?Pavel_Kou=C5=99il?=) On Sun, Feb 22, 2015 at 9:42 PM, Robert Stoll wrote: > > Probably it is a philosophical question how to look at it. IMO the only d= ifference in C# (as well as in Java) lies in the way the conversions are ap= plied. Implicit conversions are applied automatically by the compiler where= explicit conversions are applied by the user. The difference lies in the f= act that C# is statically typed and implicit conversions are only applied w= hen 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 fo= r instance). That is the way I look at it. You probably look at it from ano= ther point of view and would claim an implicit conversion from double to in= t in C# exists but just fails all the time =3D> 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 conversio= n 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 cas= es but not in all and an explicit conversion succeeds in at least more case= s than the implicit conversion. Hence, something like "a" should also not w= ork in an explicit conversion in PHP IMO if it is not supported by the impl= icit conversion (otherwise strict mode is useless btw.) > > Try out the following C# code: > dynamic d1 =3D 1.0; > int d =3D 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 dynamical= ly typed an relies on values rather than types (in contrast to C#). Therefo= re, the above code emits a runtime error even though the data could be conv= erted 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/expli= cit conversions behave the same way. > At first it might seem strange to have just one conversion rule set in PH= P since PHP is not known to be a language which shines due to its consisten= cy... > 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 th= at some value will be converted (this is something which will be necessary = in a strict mode). Why should this explicit conversion be different from th= e implicit one? There should not be any difference between explicit knowled= ge and implicit one. That is my opinion. If you really do not care about da= ta 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 provi= de the desired default value to fall back on if the conversion fails. If co= nversions 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 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. 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. Regards Pavel Kouril