Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105144 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15412 invoked from network); 8 Apr 2019 14:57:19 -0000 Received: from unknown (HELO mail-it1-f193.google.com) (209.85.166.193) by pb1.pair.com with SMTP; 8 Apr 2019 14:57:19 -0000 Received: by mail-it1-f193.google.com with SMTP id v8so16988358itf.0 for ; Mon, 08 Apr 2019 04:53:57 -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=RT3/Rk6tlIZcprnMMC/XxeE1AxQrFYmhJ/qHqWOkSXA=; b=lw1ms2xHtO7YwZlrfYT+WpsnrG9GoLmGSleEdy3AClkaoJEyEJQKjP2zymgo1kviC3 a6MzmXc/hXGTwXwdZOSDlecjltIrQgssx+ip7EnJKxlyN+2MxbRUsInXoRKcV6+1aC8S WoKCL5/A8lRH3Eqv6KA+PC06ELDnblum4n+p3Ai1tCtxeSxNdAU6elikx0zWCR1pLzk3 PHURO3wak+25MggcO7YOf8IQkHDreyO7jhjZ7pHLgEBhJJF1qz1lVTjH4KCiey1mBkV2 frQDZvTkyTaWh8oGkkhDr2vRoIHwi036NQ7vLKRdGzi0uCCyrgCtRXuZTzOo6eEDeHhy jaqA== 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=RT3/Rk6tlIZcprnMMC/XxeE1AxQrFYmhJ/qHqWOkSXA=; b=kiQl0GB1hunTjzSuxYbVx6z1R+p9ImqxzhU0HhdMCOWofWA6uXzH/MHSSlvOto1l/S Lg91HV/wrTzm2KrPqA8hmm9JVn25t9pBt4R4AJrl9dO6v2gvSzSCzKciz+5mI66N75+z 6vpE158fNURQ1vWKPohhbQ4bvI7D6Rnh8FEeOTGobqaTKw1GyBhzWCoLKybqWyrPQJLa XXoIgnucaCnrZkDEiJW/2ak/17z/2eHQ93x2+mu5So87LMXKZGFL39rBLvZhi0IaBYpd 7SWCbMUgo8AOoUSGH45x+bqfxQbd9QBnYlvaVtZEmpZCxP5pfowEbjzDzieC+8tLMFSt gmfQ== X-Gm-Message-State: APjAAAV4DjBexQK2zQUBN/4LI6xdCcQp1nPAAoO1dmEZXvh00Yvtg1wp aOVEDy0v4vTeQ+5IiitP5ob78h+vZZvXGWeTIF0= X-Google-Smtp-Source: APXvYqz+g6P6Uh8njiSIU0KJPu713vW7LffDVn+MaJ00iGKONqvznnduf7jWWcCAn4jBdUmJW/4ZhqOp/PtaGkuTWrI= X-Received: by 2002:a24:e04f:: with SMTP id c76mr20205477ith.113.1554724437132; Mon, 08 Apr 2019 04:53:57 -0700 (PDT) MIME-Version: 1.0 References: <5F014F9A-5BBD-4DED-B490-05FC681DE2BF@koalephant.com> In-Reply-To: <5F014F9A-5BBD-4DED-B490-05FC681DE2BF@koalephant.com> Date: Mon, 8 Apr 2019 13:53:46 +0200 Message-ID: To: Stephen Reay Cc: Dan Ackroyd , Guilliam Xavier , PHP internals , David Rodrigues Content-Type: multipart/alternative; boundary="000000000000cc8ebd0586037d77" Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: benjamin.morel@gmail.com (Benjamin Morel) --000000000000cc8ebd0586037d77 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > if your target =E2=80=99type=E2=80=99 is `?int` and you pass an empty st= ring, wouldn=E2=80=99t you expect to get `null` rather than `0` ? I'd personally expect an `Error`, but this is not very much in line with current casting semantics. You have a point here, there is no clear answer for this edge case. Maybe before nullable casting, should be discussed throwing errors when attempting to cast a bad value, such as `(int) ""` ? I think we're getting one step closer with the string to number comparison RFC , which suggests that `"" !=3D "0"`. Ben On Mon, 8 Apr 2019 at 12:25, Stephen Reay wrote: > > > On 8 Apr 2019, at 16:35, Benjamin Morel > wrote: > > > >> register_cast_function('?int', 'castToIntOrNull'); > > > > I would quite oppose such a generic implementation, as it might have > > unexpected consequences in other parts of the codebase : it's a global > > configuration set at runtime. > > > > I quite like the idea of nullable casting though, as I've come across a > few > > cases myself where this would have been useful and would have made the > code > > more concise/readable. > > > > Ben > > > > On Mon, 8 Apr 2019 at 04:55, Dan Ackroyd wrote= : > > > >> On Sat, 6 Apr 2019 at 08:53, Guilliam Xavier > > >> wrote: > >>> > >>> Hello internals, > >>> > >>> David and I would like to open the discussion on our joint RFC: > >>> > >>> https://wiki.php.net/rfc/nullable-casting > >>> > >>> Mainly, it would enable to use e.g. `(?int)$x` besides `(int)$x`. > >>> > >> > >> I'm guessing you don't actually have ths function getIntOrNull() in > >> your code-base? To help me understand where this would be useful, > >> could you provide some 'real-world' code where this would be useful? > >> > >> By the way, this RFC is a special case of something that could be far > >> more generic. If it was possible to register callbacks to be used when > >> casting, people could do something like this: > >> > >> function castToIntOrNull($value) > >> { > >> if ($value =3D=3D=3D null) { > >> return null; > >> } > >> > >> return (int)$int; > >> } > >> > >> register_cast_function('?int', 'castToIntOrNull'); > >> > >> $x =3D (?int)getIntOrNull(); > >> > >> > >>> Additionally, it was requested on the mailing list to consider adding > >>> support of nullable types to the settype() function, > >>> e.g. settype($variable, "?int") > >> > >> Someone probably needs to make an argument for it to be in core, > >> rather than just saying that it's something that could be done. > >> > >> cheers > >> Dan > >> Ack > >> > >> -- > >> PHP Internals - PHP Runtime Development Mailing List > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > > I=E2=80=99ve come across this pattern (string variable contains a number = or > nothing) myself, but it also then makes me think - if your target =E2=80= =99type=E2=80=99 is > `?int` and you pass an empty string, wouldn=E2=80=99t you expect to get `= null` > rather than `0` ? > > > > > --000000000000cc8ebd0586037d77--