Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110348 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22459 invoked from network); 3 Jun 2020 11:44:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jun 2020 11:44:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7391E180211 for ; Wed, 3 Jun 2020 03:26:25 -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-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 03:26:25 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id d7so958184lfi.12 for ; Wed, 03 Jun 2020 03:26:25 -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=0+M1VB4x6bPpxS2zgSTHOGjzqaoSaMwOipwHlqOWmMg=; b=sqzty4zwJTwfTVY9wGf7ycCNGBqPwkhV/8qIZosRpG+PYb9LO/btKqGDU63h7JPNT/ 6XNwyNHRVtl3a91HskjOfhcEo8zuxTm7WK4wYYSM64ASmMi2pB0ADGcca8H8tkNlfFQp GiE0MYV82GO5nipuEratHefPLzqv6zg9MZ3Er35K/Cx59IqjjVmCeCEC3Hk0kIODtmL0 uMtDd54InTx6TG6n8omQs5IrfL1fi/i/8d1HUiN/bL9dfEjLm2kik+F500Ogfv8cMtn6 y//r7o3sXSmo6S9IhAhKN3pex2eL1bMMkwO64ca5Qo+QaBBVhED/oj9QRKxLb0ylYV6j AZeg== 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=0+M1VB4x6bPpxS2zgSTHOGjzqaoSaMwOipwHlqOWmMg=; b=WNzWSFhzc1qXtfpuZgBHyT1dw7M4kB1HDuRsPD9LC9v+zoYyF7j8gtbk8SCOzQPv2+ XLv7ulq74Mss4azpNmyDgm9yyoF6fhvG6V0d3AiIWxjTjnJW26jDC7Eh/xjYl0FQAFGb c8iLJUyyIQ+3S63oTRLOVk/bM3lz6ei+xeWPtdfnNrb8FRbvTPhyD4Q+ssKJ7bb6Js2t H6Q9jG1fs2I5lPrDtfbDrLWmUczAxK6/da4rJ78yM75O0bwx6+Szm8kYjWdZ+wQbUNzV MKpRg4LHtdKAF4LKT5kUR0TZ5h5jOTf09VuJHnJmiIb6duX+2WrBa0Wvj4bo2JjyZngV hHzQ== X-Gm-Message-State: AOAM530zjC1UFDwB1PYlwztFZnAN7Edj0Au5GZNmcbOuGZ4vjm43ieQg OP09ewzgwH4GKdbMWkN9hyV6LWNX2ARDjG2xSRI= X-Google-Smtp-Source: ABdhPJzhrBKP5kx4FSK+kxftgOhDQOEIViiHXjCB5H95TngqeiFlInT8PyU/3Yg88RY0e7rvoglaOMca5kOvmODsO0k= X-Received: by 2002:ac2:5cac:: with SMTP id e12mr2112227lfq.92.1591179982893; Wed, 03 Jun 2020 03:26:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 3 Jun 2020 12:26:06 +0200 Message-ID: To: Deleu Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000a7795a05a72b75ad" Subject: Re: [PHP-DEV] Numeric Type From: nikita.ppv@gmail.com (Nikita Popov) --000000000000a7795a05a72b75ad Content-Type: text/plain; charset="UTF-8" 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 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. > 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 through to the end first. As far as I remember, it never did go to vote. Regards, Nikita --000000000000a7795a05a72b75ad--