Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81831 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60003 invoked from network); 4 Feb 2015 18:21:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2015 18:21:22 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.199 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.199 imap11-2.ox.privateemail.com Received: from [192.64.116.199] ([192.64.116.199:48128] helo=imap11-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/93-40930-12362D45 for ; Wed, 04 Feb 2015 13:21:21 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 771238800EC; Wed, 4 Feb 2015 13:21:18 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap11.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap11.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 3GvyeuPEDXLB; Wed, 4 Feb 2015 13:21:18 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 5F7D88800E5; Wed, 4 Feb 2015 13:21:16 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) In-Reply-To: Date: Wed, 4 Feb 2015 18:21:14 +0000 Cc: Florian Margaine , Leigh , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <85CB3199-8D44-4D34-9212-6E18D6BD7B73@ajf.me> References: To: Andrey Andreev X-Mailer: Apple Mail (2.2070.6) Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: ajf@ajf.me (Andrea Faulds) Hi again, > On 4 Feb 2015, at 11:15, Andrey Andreev wrote: >=20 > On Wed, Feb 4, 2015 at 1:02 PM, Florian Margaine = wrote: >> Hi Leigh, >>=20 >> Le 4 f=C3=A9vr. 2015 11:50, "Leigh" a =C3=A9crit : >>>=20 >>> What was wrong with: >>>=20 >>> function x(int $y, string $z) { // strict >>> function x((int) $y, (string) $z) { // casting >>>=20 >>> This was the best suggestion I've seen that covers the requirements = of >>> both camps, and is still very clear how data is going to be handled. >>>=20 >>> Authors who want their APIs to adhered to strictly can do that, >>> authors who want to accept anything and have it end up as their >>> desired type can do that too. >>=20 >> Because it is then the callee who decides, not the caller, whether or = not >> he wants strict typing. >>=20 >=20 > ... and apparently, this is the root of all evil. :) It=E2=80=99s not =E2=80=9Cthe root of all evil". But the two-syntax = approach has disadvantages I=E2=80=99ve elaborated numerous times. It shifts the choice to the callee, not the caller. This means existing = applications are broken if strict hints are added to existing libraries. = It means that programmers will have to deal with two different systems, = simultaneously, in the same file or function or method, because one API = uses (int) and the other uses int. It means that if the API author = decides to have weak types, you are prevented by the language from = having the benefits of strict typing, even if you want them. It also has = a syntax issue: (int) looks like an explicit cast, yet what is usually = proposed doesn=E2=80=99t something similar. There=E2=80=99s also the = consistency issue in that we=E2=80=99d have to support real for = consistency with (real), since T_DOUBLE_CAST is a token. It means that = internal functions could now also use strict hints - or if they can=E2=80=99= t, we have a consistency issue. It means beginners, and people who = don=E2=80=99t like strict types, now have to deal with strict types in = their code, despite PHP always having been a weakly-typed, = beginner-friendly language. And so on, and so forth. The current RFC deliberately does not propose this because I think it = causes more problems then it solves.=20 > I am baffled by how the two-syntaxes suggestion is always so easily > dismissed by that argument. I'd argue that most people who support the > current proposal don't fully understand what declare(strict_type=3D1) > really does. Well, *I* understand what it does. I=E2=80=99ve used it. It worked = really, fantastically well=E2=80=A6 better than I expected it to. It = provided benefits that the two-syntax suggestion wouldn=E2=80=99t have. = It provided benefits that just strict hints wouldn=E2=80=99t have. It = provided benefits that just weak hints wouldn=E2=80=99t have. > As I've previously said - putting the caller in control > (and really the caller, not in a per-file context) makes it a > debugging tool, not support for strict typing. What=E2=80=99s the difference between =E2=80=9Cper-file=E2=80=9D and = =E2=80=9Creally the caller=E2=80=9D? The RFC *does* allow you to use = declare() blocks if you really want to. Also, type hints are mostly a debugging tool anyway. They=E2=80=99re a = tool for catching errors early, and to enable better documentation. = That=E2=80=99s all that they do. -- Andrea Faulds http://ajf.me/