Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110341 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 96934 invoked from network); 3 Jun 2020 10:08:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jun 2020 10:08:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 87BB5180088 for ; Wed, 3 Jun 2020 01:50:59 -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_H3,RCVD_IN_MSPIKE_WL,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-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (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 01:50:59 -0700 (PDT) Received: by mail-ot1-f53.google.com with SMTP id m2so1237601otr.12 for ; Wed, 03 Jun 2020 01:50:59 -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=XFATE8kCwsPtXa4C7OAvYbnbJ9RuZq5JBGnl68QvF9c=; b=JKuvjupfgeZ2dehNuJosvAFbXaNkeDj/6Zw+ssOvOwvGM8VXDA7RuI8xK7l3u4y7NJ Y95qnflaC89xl0c1/qfxtBAxuaBbv4UI7qMguT8kRy9vf3qq61TKgNVZdjwMdjrUp79q 7NlbjMUy1D4kTnYGelMVQmRXLLndov59bBgEG8Kf5bcttNb1Pbcu5iXUvZTbEzs9r+Pi lPD99s6Y4qmrMhMhTRT/ImIvfdMKzju2m9QfZsaB5Thr6+4mdXsYec/fOeZoLVx7XRRR HTzOeamrvwddkzsH2INjsUY9O8bnywNKdsZQKqneTKT3+pwWD9sZbKkjIIvUMAzHnzbu IxQw== 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=XFATE8kCwsPtXa4C7OAvYbnbJ9RuZq5JBGnl68QvF9c=; b=XzSwl6KFhj0Y64dtTyjfsTQqyBB4CyWTfK08jdzSvfajPAaT03IRWZ873FyvuMrzk7 7GD+VPIUFKN87g2Bxbl82QCkDCNK51cnCNyDbPjwG5dYL9nqJ1Fq7Uwzx+sSzeuXBVLR jQV+Q9OKEyhGSShWWa5eG0gEVEtVg0xMb1deUpvhp0P6K5ruPFanQ3GTlBcSBpsYQ6K8 pbHjIkWJHm9WcQ6lMKC/wkHlp7sFpXrnKUTFmTkW44zGXveY7J7riUbEtWmikJdqyy6Z rd8KFcuy8G4+cVV3Ym+i/dCxqoVJPiDDBorCh6iF0R7DjidypPht7oas5WaxjXoC2+h/ A1Rg== X-Gm-Message-State: AOAM532s5ZEvOjLkaMI9Vv61qSvVFAN2rRGNOHOJzJpmMjiFv7Udsdmi WrArS9JHvYqkArXsp3+0nPc+1Ve0XfaHAWHk3tY= X-Google-Smtp-Source: ABdhPJxJr1Qw94/UtBvRdVhojvjN/iZCU2a9SEl7K5ZdOBN89xxMyH/cXn0ce3Idm3RSKaGm5/iI97AQBt+BJKmp0v8= X-Received: by 2002:a9d:d83:: with SMTP id 3mr2290113ots.365.1591174258460; Wed, 03 Jun 2020 01:50:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 3 Jun 2020 10:50:23 +0200 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000738e6605a72a2060" Subject: Re: [PHP-DEV] Numeric Type From: deleugyn@gmail.com (Deleu) --000000000000738e6605a72a2060 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Rowan, Although I understand your concerns, I wouldn't consider them a roadblock for this feature. My rationale is that `is_numeric` has been with PHP for so long that I don't think we would ever be able to have a `numeric` type-hint that doesn't align with it, which imho means we either have it or we don't, a 3rd option where a possible type-hint that is inconsistent with the function seems worse than not having it. I fully agree with your concerns that it might not fully represent what the developer wants, but I would make a judgement call when using it the same way developers have to make a judgement call when using `is_numeric`. It might allow far more than you want, but there seems to be enough use-case that it fits where the downside is negligible or even insignificant. My point is that I don't look at the language validation to protect my code against unexpected values and I can setup validation to deal with that. What complicates things is that I want to avoid having a force-cast which is not safe for my usage because I want to preserve `null` while not having to disable strict_types. On Tue, Jun 2, 2020 at 5:47 PM Rowan Tommins wrote: > Hi Marco, > > On Tue, 2 Jun 2020 at 16:10, Deleu wrote: > > > > > 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`. > > > > > The problem with is_numeric() is that it uses an extremely broad definiti= on > of "numeric", which is not often what you actually want - for instance, > is_numeric("-2.56E-90") returns true. > > The case I frequently see is much stricter: the variable should either be > an integer, or a string representation of an integer. Whenever this comes > up, I struggle for what the correct check actually is: > > * is_int($x) won't accept any strings > * is_numeric($x) will accept far too much > * an int type annotation with strict_types=3D0 is similar to is_numeric; = in > strict_types=3D1 it's the same as is_int > * (int)$x will never give an error (meaning it's oddly easy to accidental= ly > _suppress_ errors by trying to make code run under strict_types=3D1) > * (int)$x !=3D 0 is sometimes good enough, as long as zero is not a valid > value (if you use this when accepting Unix timestamps, for instance, Jan > 1st 1970 is going to be rejected!) > * ctype_digit( (string)$x ) is confusing to read (the cast is required to > accept actual integers, and the name is odd out of its intended context); > it will also reject any negative numbers, which may or may not be desirab= le > > The only suitable built-in function I'm aware of is filter_var, which has= a > pretty horrible API: > > if ( filter_var($x, FILTER_VALIDATE_INT) !=3D=3D false ) ... > > In the common case that you want to cast the variable to int on success (= to > pass to a strictly typed function), you would need to do something like > this: > > $x =3D filter_var($x, FILTER_VALIDATE_INT); > if ( $x =3D=3D=3D false ) { > throw new TypeError; > } > > I would love to see the language have a built-in short-hand for that, in > the form of some kind of "strict cast", e.g.: > > $x =3D (int!)$x; > > or: > > $x =3D (strict int)$x; > > Once converted in that way, you could safely pass to a parameter marked i= nt > in strict mode, so wouldn't need a new pseudo-type. > > > Regards, > -- > Rowan Tommins > [IMSoP] > --=20 Marco Aur=C3=A9lio Deleu --000000000000738e6605a72a2060--