Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123735 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 AE0E01A009C for ; Fri, 21 Jun 2024 16:12:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718986445; bh=1TK0F3kIy6KDB6NLdjj2eDYLnUK/ibjOgmvqYh6UxoA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=SKpXNU523DGIwBRBEkcc3E5knTRU+BYDWaBZ70LaCbxLxAm/3xecEckb8kjenDz9e 1ee0h0uWkzVzXKiDKMsKnDz+M/xaTixc/JJc8iG8h3+NcVulvR40h31AqZZByt8cd2 y1qORvNQv9JeRlA668jvLneo7KyJuX+DdamNNnYqXUi37YWDhBqW96H3M5UVTcVTTx UzfBrb00J+bUdp+V9Jav2vuTRIkT19GCPYJ0uUKMg5G5xtxfaOjgc6Lg83TqHKHy3u 0nCFjDYnsNqeIuLzECPi7jnvE0Y76kJXz2Pb7UjKgQZZ/XM7UgOozZeDdBHhokt1SX 7l9u0qhyznzaQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6D845180777 for ; Fri, 21 Jun 2024 16:14:05 +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-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 16:14:04 +0000 (UTC) Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-52c525257feso2684477e87.1 for ; Fri, 21 Jun 2024 09:12:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718986369; x=1719591169; 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=y9+FCy6w+ivuglo7HlBUsT+JRSYY+p6xfqDVOBiWPUs=; b=CWIro2WYNWSR/uqD3mui8QcyiY+zzWUwUHMvTD4uDzGnnJuhVWDdHdDFbmOz6I29aK jxudRL2tlYwAulJ6pfMtQu7fczhY4r9xsvnlKhjgQQgM7/Wr2B+vwEqw9szC33BI7rrM jExfN5R5M08BtmYSwdyonQnLRQnf+n7X+Jb9vFWzxNJeyTS3ipFf3Pbth4prdZtgzqbq /EQnLv1ttfiKe9FsveR0Et4xpYQHv4qJyzUytXF0o2l0h5rSGKmuhjJcJEy+9YsbMtE+ nk3lnxUyLueEk93uuDOR0R9vyrjC6CFPfF5LcqYWCAEyvO638MYNIw+oBqLelwTP3Zre qFgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718986369; x=1719591169; 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=y9+FCy6w+ivuglo7HlBUsT+JRSYY+p6xfqDVOBiWPUs=; b=abEs84DsclRPoajtroVND09xgsuRQTF6Sy8VwSC2wjgC+EtFL+eF6z49LZArTTSZCh KQO0Txh0yVZE74cIPBVRQZsTfL73eEBDmls4CPDwauMvtQNDDykh6WkUZfMFaevDquct aBRvA9ikSZJKZSo3whkNL5mN1ulOVf5IOFMTLkQfl87BQ2UNOyVKwT6Ag9/soHgGWTW2 BWG8dmdq1wPRPOaTJfwDaeDo6FduI4m3HQ3tyakHbNgHEpPC4zVeqPdgWxDB8wobZFUV RBZQ51KmSuMvGKtKCguenCg5WGl6SCmizlpRzPBsrRxSv3Kk1YjnLzhM1Yc4cN57Gy0n hWjg== X-Gm-Message-State: AOJu0YyjTanGJpySPS2HHAUlrr7DDmq9oC/DervXp+H1Wj9JNG3MYJ1w hyA5/oKTf/vHC7nkpkcViAnsD5lodINzPskGuVUTxzndO/unEU0e41kvjOAueUdXA5bLsbfEACP TZaUbvCegkbjd88fQgHL9CWcFmVBQonjD X-Google-Smtp-Source: AGHT+IGWHY6rb7uhyjkiTcsSwdKjXXKt42GMki0yl/PRtZ+LNtWVLmz+4J1ss/5B0UmDumzJ+90ZfTxMeA5ekrEF5EQ= X-Received: by 2002:ac2:4143:0:b0:52c:b227:1503 with SMTP id 2adb3069b0e04-52ccaa36951mr3994057e87.31.1718986368695; Fri, 21 Jun 2024 09:12:48 -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> <914cde73-3e53-44a8-94ff-bcf34000cc0c@processus.org> <84be04f2-73cd-4fdb-929d-724d4fea98fa@processus.org> In-Reply-To: <84be04f2-73cd-4fdb-929d-724d4fea98fa@processus.org> Date: Fri, 21 Jun 2024 18:12:36 +0200 Message-ID: Subject: Re: [PHP-DEV] Renaming "strict types" to "scalar type coercion" To: Pierre 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 5:49=E2=80=AFPM Pierre w= rote: > > Le 21/06/2024 =C3=A0 15:57, Robert Landers a =C3=A9crit : > > On Fri, Jun 21, 2024 at 3:01=E2=80=AFPM Pierre wrote: > >> Le 21/06/2024 =C3=A0 14:27, Robert Landers a =C3=A9crit : > >> > >> 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); > >> > >> And how about: > >> > >> some_function_expecting_int(\intval($_GET['foo'])); > >> > >> And moreover, I'd write something like this, but: > >> > >> function validate_int(mixed $value): int { > >> if (null =3D=3D=3D $value || '' =3D=3D=3D $value) { > >> return null; > >> } > >> if (\is_int($value)) { > >> return $value; > >> } > >> if (\is_string($value) && \ctype_digit($value)) { > >> return \intval($value); > >> } > >> throw new \InvalidArgumentException("What what!"); > >> } > >> > >> some_function_expecting_int(validate_int($_GET['foo'] ?? null)); > >> > >> But the example might be erroneous, I see your point, nevertheless mak= ing coercion explicit doesn't seem really relevant to me, the one point I l= ike in your syntax would be null handling. > >> > >> -- > >> > >> Pierre > > Or... you could just turn off strict types instead of reinventing > > coercion that isn't nearly as well documented as the built-in > > coercion. > > That was one of my points actually. > > I don't see the use of a syntax such as `$foo as ?int` since all > existing types of coercion are already possible via the cast syntax > (please correct me if I'm wrong). > > In the previous message proposal, the only neat addition was the null > handling (that's said, that's a really cool point). But cast syntax > `(int) $foo` is really equivalent to `$foo as int` in terme of concision > (less than 2 chars diff). Well, $foo as int doesn't exist (yet), so there's no telling what it will be the same as. > In almost every case you would need coercion you also need validation, > and validation is not something that's possible with a neat syntax, at > least not this one, because validation is a business matter, not really > a technical one. That may be true, but isn't always true. Other than how it is represented in memory, is there any difference between "123", 123, and 123.0? That's what coercion does, it makes it so that the actual representation doesn't matter. The only thing you really have to worry about are the edge cases (for which there are many, but that number has slowly gone down over the years and none of the remaining ones are truly surprising, IMHO). And if you are just going to cast a float/string to an int anyway, you still have edge cases (null, no warning about information loss, etc) to handle. > Regards, > > -- > > Pierre