Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110444 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27565 invoked from network); 9 Jun 2020 10:54:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2020 10:54:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E85A71804B7 for ; Tue, 9 Jun 2020 02:37:49 -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.7 required=5.0 tests=BAYES_05,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-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) (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, 9 Jun 2020 02:37:47 -0700 (PDT) Received: by mail-io1-f54.google.com with SMTP id r2so22016852ioo.4 for ; Tue, 09 Jun 2020 02:37:47 -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=rylHXbjN3wCV8SxixBHBJQHE4EMHCuTKQm3dob0xYvQ=; b=YtXI9NYnt0UySYvjEgbzhhPLokleTZ3eQif/csC80mivRHhARBfAv1q+K6x0xI3609 CI7+yKltiUr4wv0+La/DQldqcEihl64eVTgu7khzEr9SZc6aIYb41ixF7/1K8CmYwnuu 1M5iIzB3GsHOA4t+fV51oJbf3CZOcr8d7W9gYzoO/N6Rq54lixZJeVhdbDiGqrfX+nXd BtmbU/PQRl0/92quhrmlb3oO5Iaydu7GgQcxSMz6+bYymsZk72VzAr9JT3EhBr5ndHA+ Tzk+rsFsFbL90TGcKMIur1BlK+7+/er31NZxhkdkNNkJ3XSMkQ5yfTIAC28KvTd456ya o+PQ== 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=rylHXbjN3wCV8SxixBHBJQHE4EMHCuTKQm3dob0xYvQ=; b=HAU35u4S3BbSMxpfUdxziRFn4tuH0rcVP6QsZ3YCarbbeKHUYM8rtzB6QwvoYdyA2E 9TsvI50p5pPAOgzEtSEeNP6mSiQFfhTONHTh0f1k3VIXkrmx1KBgzVNGIEIaH+0sqHCo Vhssc1KQbvWqw4UTipJ7G3Ngm89PtIRn/0WaTcNVorGQA9qzLySgj9VJpB/Z/OgcR5Qs YNlk+octt6QCN3JRogP47x28JMYcrIxVkWxbv93jvNewjGAeinASK+X62u5VQQsoorhN 1MOC41S0czpYATLbBHSJQbOjBOp5AF1EY+nEOXL6xCcqr2qhiH41lEG8CGtT1OejyaN9 QakQ== X-Gm-Message-State: AOAM533Q3JBvIJchjOsImDRRAn5MNSru1qoaIRqp62xfdNi5tfHbxOxl QnZ8NwzxZ82XQ2r4v0tH5BlAsIRKha0aA3261ZE= X-Google-Smtp-Source: ABdhPJx/jsk/7vRfwz8xfGG3/Tx2DOnyJjO8lx+2/aFH8OJFHYqOnzV7oyQjzjKYbpiE9MnkCNewtUS9E/LdNp7f0r0= X-Received: by 2002:a6b:ee15:: with SMTP id i21mr26873457ioh.179.1591695463117; Tue, 09 Jun 2020 02:37:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 9 Jun 2020 02:37:29 -0700 Message-ID: To: Deleu Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000ab788605a7a37acc" Subject: Re: [PHP-DEV] Numeric Type From: sarkedev@gmail.com (Peter Stalman) --000000000000ab788605a7a37acc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, One thought is that `is_numeric(true)` will return `false`, but the `int` type-hint will accept it as a `1`. The other thought I had was no one mentioned floats, so maybe a `number` type could be argued for but with mixed types that can be solved by `int|float` now. Thanks, Peter On Tue., Jun. 2, 2020, 08:10 Deleu, wrote: > Hello Internals, > > I'd like to know what would be people's feelings towards having a `numeri= c` > 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 t= o > 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 come= s > from HTTP, CI or Database. Whenever interacting with these data providers= , > string is a common format. `is_int` will not return true for integer valu= es > 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 give= n > 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? > > -- > Marco Aur=C3=A9lio Deleu > --000000000000ab788605a7a37acc--