Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110350 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43842 invoked from network); 3 Jun 2020 13:18:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jun 2020 13:18:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7E2BC1804C2 for ; Wed, 3 Jun 2020 05:00:41 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f174.google.com (mail-il1-f174.google.com [209.85.166.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 3 Jun 2020 05:00:40 -0700 (PDT) Received: by mail-il1-f174.google.com with SMTP id i1so971043ils.11 for ; Wed, 03 Jun 2020 05:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=URB/7qVPaoU/euR5bThwM0v7cJuvtn8imKyFsnjO5/A=; b=A+CSiEBlECy15VrT96pdSbKKBMbbMsfcQCw+/Z7SOyAgZkfJrmdQCEMfH27FSpuj0n w8lzOY0R813Lmvqm0wBq3SjZ6hKcZwrWXsKUWfShg5EYtv0QkO4BSP6vXM4itP1IhvV7 zzd6+gPZFTw1MAECvFyUVIJcox2sea0L3uYn5m2OBjSq/GYNyolIN3QV2LBWu1yCpRvK 6pgtd3mRatSWRy8rijn4RBvjr2wlpQp3O1Ik2labjNKHHT4QaT6pgfBBTtNq5XnAHY7P dx5EUZlSrF8dwAzR1iPq6K7SxVmc5SXJGdxNs4+VW4S4GWvQimq0xCxXRt5DIsezRjCO aOww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=URB/7qVPaoU/euR5bThwM0v7cJuvtn8imKyFsnjO5/A=; b=RQvWcT+3QlfY4UBq5jtPQ+Q+RuDAHVoyO2kOXAewFEkH399nJ5itgZRdB5k70G1kFz PhCKHb30Zidyn4Ktq+iqCLIQcjXDfClN5B7WVn1b7V0RAnQBrmSRRKlMWIMHca/gqjB8 zzvqfJH3efaUCf/0+aDT3XQxQWLHWluQo4/EcjUi/w+IpvzEBy5bkwhYFSDNElFwKZOx e6Ta9d7hWsI7d2KeTpN0ZY2mkgHvih74wO36njv3l+XtCeaHNOQS6rNCjm734GHX9wjI ERtaIAeKMHARDUJBDdHEROkdfFMe7jXgAg0eXWKuoPA32R9kq2EevociS8gqT+g1zb3G paHQ== X-Gm-Message-State: AOAM531eE1UAljwxrOjDpTuaqPBwWQVCCPughX5Zp+Gv0EC1ybYDGd1H ttiFNJ3/PhFIbuXJ3iW9CUSoT4ckwkeA50LghUA= X-Google-Smtp-Source: ABdhPJxqEaXLBAojwSX2tVn0I/lbMqxFmtbdw44JM5PoSj206zpB/mnxbkcx23B74qJ/Dg3gq7QpMRv2XOlP+0ZhQsw= X-Received: by 2002:a92:bacb:: with SMTP id t72mr3527256ill.26.1591185640479; Wed, 03 Jun 2020 05:00:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 3 Jun 2020 14:00:04 +0200 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000df62a005a72cc68b" Subject: Re: [PHP-DEV] Numeric Type From: deleugyn@gmail.com (Deleu) --000000000000df62a005a72cc68b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > To clarify, what are the semantics of numeric? If you have a numeric argument and "42" is passed in, will it be converted to int(42), or will it remain as "42" (as is_numeric("42") returns true)? I imagine it would remain as "42" since implicit casting is the source of some of the problems with the PHP type system. To be honest, I'd be fine with either, but I imagine that advocating for yet another implicit casting on a specific type-hint would just lead to more edge-cases and not be treated as other types are. > From your description, it seems pretty clear that you really want the nullable cast instead and "numeric" is just a workaround that makes a specific case tenable. That does not seem like a good position to argue from. I think it would make more sense to see the nullable cast RFC through to the end first. As far as I remember, it never did go to vote. There's no denying this, however when I looked at the discussion, I got the feeling that it may not have move forward due to the arguments against it. The reason I decided to send this email even though it doesn't seem like a good arguing position is the fact that when I looked back at all the times I wanted nullable casting they were all `int|string` scenarios. If a cheap feature such as `numeric` type-hint can solve most use cases, I'd be more than willing to deal with the custom code I will continue to have to write for cases that are not `int|string`. I don't know if there will ever be an interest in deprecating "42" + 10, which correctly results in 52. If there is, I can understand how the development of this feature might be counter-productive. Other than that, I think the benefit of this feature outweight the cost specially when looked at it from the `is_numeric` perspective, which is an existing behavior. On Wed, Jun 3, 2020 at 12:26 PM Nikita Popov wrote: > On Tue, Jun 2, 2020 at 5:10 PM Deleu wrote: > >> Hello Internals, >> >> I'd like to know what would be people's feelings towards having a >> `numeric` >> type. I remember reading the nullable casting RFC ( >> https://wiki.php.net/rfc/nullable-casting) and it's discussion ( >> https://externals.io/message/105122). Although I would very much prefer >> to >> have nullable casting, it seems to be a bit controversial and perhaps >> Numeric Type could be a middle ground solution. >> >> The primary intent would be to safely pass around input that usually com= es >> from HTTP, CI or Database. Whenever interacting with these data provider= s, >> string is a common format. `is_int` will not return true for integer >> values >> that are string, instead we need to rely on `is_numeric`. Similarly, I >> think we could benefit from having `foo(?numeric $value)` as a type-safe >> mechanism to transfer data around without having to forcefully check for >> numeric value before casting. If we simply `(int) $value`, we may end up >> casting `null` to `0`. Type-hitting `?numeric` could be a compromise. >> >> ``` >> foo(?int $number); >> >> foo($_GET['param']); // TypeError: foo() expects int or null, string giv= en >> foo((int) $_GET['param']); // null becomes 0 >> foo($_GET['param'] ? (int) $_GET['param'] : null); // expected behavior >> >> bar(?numeric $number); >> bar($_GET['param']); // would work with any value that passes >> is_numeric() >> ``` >> >> I also think this approach have a benefit over PHP 8 Union Type >> (int|string) because is_numeric() does not return true for string values >> and for consistency the `numeric` type-hint would behave similarly. >> > > To clarify, what are the semantics of numeric? If you have a numeric > argument and "42" is passed in, will it be converted to int(42), or will = it > remain as "42" (as is_numeric("42") returns true)? > > From your description, it seems pretty clear that you really want the > nullable cast instead and "numeric" is just a workaround that makes a > specific case tenable. That does not seem like a good position to argue > from. I think it would make more sense to see the nullable cast RFC throu= gh > to the end first. As far as I remember, it never did go to vote. > > Regards, > Nikita > --=20 Marco Aur=C3=A9lio Deleu --000000000000df62a005a72cc68b--