Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110334 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68017 invoked from network); 2 Jun 2020 16:28:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Jun 2020 16:28:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7D7EC18055D for ; Tue, 2 Jun 2020 08:10:17 -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=0.6 required=5.0 tests=BAYES_50,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-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) (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 ; Tue, 2 Jun 2020 08:10:17 -0700 (PDT) Received: by mail-io1-f44.google.com with SMTP id p20so11125265iop.11 for ; Tue, 02 Jun 2020 08:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=qEFvcmnCxSDBgcPLSPVaicd4p73sZGTqIeK38jIDjfs=; b=e6MT5zRQpI+1ykw/AL1GmHSMwVDziI1lv/OAq8ske/lYXo1sVDqEAZLoyxSxNb1n5o RqkZqiGi1XS+wGZJVcKSrD9uzTfLgGgF+WMI7CRUZLscFBO3508g+KVsfX+9etR95QfR LUdTNSnoJOKl2kD3hXWHQawtiBWLq6hyvh89H+nCK+v3sd/3S6cVBIWEWgExyvZUBywS F7/w2kJfNMOdvDKyH4n5v3NGnMhDgaEwmwL0JWJ5wDQvvEPJIRaHNtxRZVkAy8jzbrSI 9tprS0vvXxbucywDXnfGDV594MBsOIobb8HfKvjDTkX1E+v4DQMIwm74J0KptLkKf+dc TS6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=qEFvcmnCxSDBgcPLSPVaicd4p73sZGTqIeK38jIDjfs=; b=grEIy4AAP1QXn7T9A0NkZByU1fQh0pzqYakMTxhPtNe1ulE/Bg/nQfGy/qEy7Hd7MT dcxJdfVdPDog+xlMVy/PU5bkRKKzit270mM8A6/KNvDyIR1EtyzCe6Q+SsyXBtvqO9iV VGhxvn3m4MMpklU8jFMh/w53/73iuIdc7HCbox+mSGMTVDDzbqWucDFPlZ8Qhm2VQxJY TI49SNv/W0W0V5u5ZFbo7BM7TUU3iVDOB+6ymV1gH3JQW6FLhwTVNDVUpV5xhQby4ays /9Zc6ivlGH96Z9jIHlzoAkc2bcku8Fl2zL8Sz/kgqtvqgIp0waP7rCMBpRJaxZWkqaNB RNog== X-Gm-Message-State: AOAM532So8yKlc7OQbS6Trpmup9EZS+OVokwtKKkdNXPjNeLfFC2tyHy Nxj3/AKQeqfML97twaPwkIQfSD3om94148ufk8y6dvCDygs= X-Google-Smtp-Source: ABdhPJwttvvBBDXWsQZYqfd2Qu+g79s31X0RJeY+OCvhR7Rwm9tOAUvi+voRsZ9rYbfXIF5dNHaTwW62Ju/qpSlVwCc= X-Received: by 2002:a5d:9495:: with SMTP id v21mr5473908ioj.26.1591110615692; Tue, 02 Jun 2020 08:10:15 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 2 Jun 2020 17:09:41 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000000bfa3905a71b4ff1" Subject: Numeric Type From: deleugyn@gmail.com (Deleu) --0000000000000bfa3905a71b4ff1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 comes from HTTP, CI or Database. Whenever interacting with these data providers, 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 given 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. Thoughts? --=20 Marco Aur=C3=A9lio Deleu --0000000000000bfa3905a71b4ff1--