Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123724 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id A48E01A009C for ; Fri, 21 Jun 2024 12:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718972944; bh=zZSyPDV5vnX31QsJL69SwJpS4LJT18UBpj6uKGgdLqM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=l5+s/sG3bXfpi4KPz0Re57QFJZT/pcyj6FaXg2IBARRtmrrNQ4piJqalI5KG+otKE PBeuerCn0fPH3sFiy2/Y4WqCpY6l4C+7VzXUFb4Xq7kgwEWCMJBYVpvN2AYhP5D1QQ /abOtFy/6MWAYuvqxeJ6OUTijdqwi5CYCmDBukNBxrzik0xxaeDEQhSZJ3r4XUyv4w L4xPsDjORSnswquoS3tGYO+gX57QuwG24URQCZLnYF7S3aLi8uxXGaoJ5wTNUC3wmS lPILJzeVmLvmNB9cKD6Mv7dxqYiidmtF6mCP/UJpX8AjH8l+P4QrDIdkQxs5zJUwbl XGqKpSsj6jvNw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B692518007F for ; Fri, 21 Jun 2024 12:29:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 21 Jun 2024 12:29:03 +0000 (UTC) Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-52cd790645eso516277e87.3 for ; Fri, 21 Jun 2024 05:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718972867; x=1719577667; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Vt6p86YMXJ+EGViKqXfyY7wYTDNJB73LzXjNeadCClE=; b=KeOKJruexAvvwPH0iElUIcdB6duF6rF7XlDPif/lxofDFIDELDh9IeVZcNeZC4Mnpb aXDDGDKdgoj3Lxz4/j9dmHMrYrZsyD9kPxO0fkCP2iPL6HldcqwCDXXrWQl4i94r5Ifh PJcS8k+lX4hIsikzvv1Qf/pNm5rF34a2OGSwz8Z2UxWjv+1CJX3Kyhx63t9uEU8Sxwdn YQUIEeDmCz1pHRZ1GLu4+Ml06D0FnIOn1eCP3sBkDeh9drs9RuCdgtIQmowD0bv6Z78b NAbHQgzmPTJPeRBX4KgrkBOqmBDrtAGfESO42468hQp8DRb1e3uQ/KVExFJ7GO4PLTCM lVJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718972867; x=1719577667; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Vt6p86YMXJ+EGViKqXfyY7wYTDNJB73LzXjNeadCClE=; b=u+XEBlT4jmjaY68sPv/Dj17aoUhMvZXhlH6btZBY/hYHUMBV4ckVdSpEDdba0BPjxK A4guSVstMnhGhVAatzQ2K34VWiFDplBmQO9cfxTOQl71iFwOvrlyx8ixeqZMeSSgu5RL fUAnCPOJkmtE41pXh5prNmHLFJNgPQ7Hj4YEGyl7nUGGMFv8B8p+1NG4KZiRU8ASXgut tbfrpp3vaiAdlDK6D1LeAegN/J43PuHfsaV0K/f8qDGOngnw0PCXIlvKUzognZR2dgbz FfErFfDh1tSIj0R6sVTx/PuMy8zjyjumj0JgJZkRFFPILtUO7HD8vfLpJZOnMyYmDlTD 6vbA== X-Gm-Message-State: AOJu0YzauZdbzLm43ItzMNUxt8o8qY9rs0F+ucJYj7FeEc0S8LwnORWO 1cI71ztq2GzRY1L6fITxGbFmSuAOlIHWEx5DsnNHVtwD2IcUfogwlUw2s/kHVcwNdWVn39Gllig TsBSz0cmOrLuiLAIdl/l7S9TJpGSB4mCse5A= X-Google-Smtp-Source: AGHT+IE/2E58md6UFozZpikEMnwPeML0KAA8Gl7WHnMXAx0gnvv3rlQoFvHsqGYFQ9jOmlUl9yUtoZzpqOvstAb8aE0= X-Received: by 2002:a05:6512:3d8c:b0:52c:d8a9:a22f with SMTP id 2adb3069b0e04-52cd8a9a295mr1118435e87.9.1718972867067; Fri, 21 Jun 2024 05:27:47 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <9d28c9tOCChpWUcP1SFiFwtf2-w2XAtDzHsmM73myA7zfBsn7TxxEqs-VCnxRAgZjsk2TxS55owmM8wSLgBQL8LgeQLntUQ9toeLMkjTVx4=@gpb.moe> <673a1803-83c4-44b4-bd73-4ba61e83412e@app.fastmail.com> <350c1531-9bcf-4e7c-824b-8d9904b9e48e@rwec.co.uk> In-Reply-To: <350c1531-9bcf-4e7c-824b-8d9904b9e48e@rwec.co.uk> Date: Fri, 21 Jun 2024 14:27:34 +0200 Message-ID: Subject: Re: [PHP-DEV] Renaming "strict types" to "scalar type coercion" To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Fri, Jun 21, 2024 at 12:54=E2=80=AFAM Rowan Tommins [IMSoP] wrote: > > On 19/06/2024 16:34, Larry Garfield wrote: > > Also, as someone who does put every file into strict mode as a matter of > course, and appreciates the many languages that do not even have a > concept of non-strict mode (like Go or Rust), I really don't appreciate > the backhanded comments in this thread about people who, you know, care > about type safety. (Something that weak mode, when it was introduced in > PHP 7, only marginally provided if at all.) Strict mode prevents bugs, > full stop, and we should not fault anyone for applying that bug-stopper > tool liberally. > > > Used correctly, it absolutely does. Used incorrectly, it can actually end= up *hiding* errors. > > I've seen lots of cases where code like this: > > some_function_expecting_int($_GET['foo']); > > Gets changed to this: > > declare(strict_types=3D1); > some_function_expecting_int( (int)$_GET['foo'] ); > > Even in PHP 7.0, the first version is actually *stricter* than the second= , because explicit casts never fail, but parameter coercion (mode 0) always= failed for strings like "hello"; as of 8.0, it also fails for strings like= "123foo" and "". > > And this is exactly the kind of code that coercive type hints were intend= ed for - HTTP is a text-based protocol, so most PHP applications are dealin= g with string input and output *all the time*. > > One of the things the language badly needs, and I have been trying to com= e up with a proposal for, is strict type casts, so that this code can actua= lly be written safely but still concisely. This is why I wanted to work on "as" part of the pattern matching. It isn't clear what will happen with the actual pattern matching RFC (yet), but being able to do: some_function_expecting_int($_GET['foo'] as ?int); would be super nice. I think it just depends on if we wanted to go the C# route (where "as" =3D> null if not an int/null) or throw an error if it isn't one of those types. The more I think about it, I kinda like the C# way, but I feel like the error way is more idiomatic for PHP, which is much stricter on nullables. > HTTP is a text-based protocol, so most PHP applications are dealing with = string input and output *all the time*. In PHP, pretty much everything starts as a string because everything starts as a series of bytes, which can only be represented as a string in PHP. There's always something that has to convert those bytes into more useful information (whether that string is human-readable or not). Sometimes, a PHP extension will do it for you, and sometimes, it won't, and you have to do it yourself. Doing it yourself is usually where you don't want strict types (if you trust coercion to behave as documented) or want more control, so you turn on strict types. There's quite a lot of footguns with strict types (like the one you mentioned), but also others, particularly in library code that accepts callbacks. Robert Landers Software Engineer Utrecht NL