Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105137 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 25855 invoked from network); 8 Apr 2019 08:08:45 -0000 Received: from unknown (HELO mail-it1-f177.google.com) (209.85.166.177) by pb1.pair.com with SMTP; 8 Apr 2019 08:08:45 -0000 Received: by mail-it1-f177.google.com with SMTP id u65so18903805itc.2 for ; Sun, 07 Apr 2019 22:05:19 -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=epaXf8N0NzuW/DiUSXF7BCmoz0fGagh3fC7UtR9sFEs=; b=juHpc2+i/af5X5oINU39pGGqpXlLyApZ+vQx4ozb6s/6IFAXYJa2wq3oHZhG2uA0ga it8SSSsfAvIqFCVR7LL69GfEZdfc4ZhZFxtB7QOAdphffeuaN0bsDZlIguTgdgD84XCB Fey/zPa5uinF1bmVPCuj8iU20CpcIHo624lWGHPvvXmX936ZNloF52Klj5NBcA5bUavw VzNMGAM7ERQWwn28/nf/Yl25BfBcOHn8d04jHs0ueTTWcnRi33LPWgEbKYJv2fcyptJy aFusThvXBkF0fMk5p4mCYUvGY49b8eePMFAdJQbfEHk/JhvXkH+MvSGlfJhKbyS1jT0l vLRg== 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=epaXf8N0NzuW/DiUSXF7BCmoz0fGagh3fC7UtR9sFEs=; b=uF/+BYr/XXYIP9Mu+G+rtb9GlaoUqKd3iSqbvEtNS9wD3k6q1kHaaTsV5wzPj0ansL it3UxRvSgKl+mEz1tDBT9aDCEfF3vJFvhoKHyNXZpMjAFe2wtZ0lkdMKdyf4b4S0U5jO Bc88toae8kVzFNFYursj1DN6QbL+ibU/Jz3vweRG7jME8WYXu8reRCaaZ9r7hM2SeSYM rdYqfl+R8P/Jms/HA5EpDzvf+h41jSyBb5bWM06r1Rfv+1E4wqqedVAIvMej5+lODQpJ lk38ZXJIQfsCa6QVEh02VCMwf7GFFmw8AwNhifchZP62RV/w3joPeCiYOo0vMwevgpyD jhmw== X-Gm-Message-State: APjAAAUKgq1Eu9Qs7bwmb2QUz2amrc/NC6mtAhXtO2xv62ROW/vpzF33 AaLIkRXrNLzdyD/1Wc7OL9QudGmdJXFTZtWcCTE= X-Google-Smtp-Source: APXvYqwi8FhkgYJAYIaTgnshZqxANnoCXYJ4w8XhmbjjVbUbo8Hb4eCSK7ri2TjLmKyKuZf+FeLIeeSRiyNe1KzRSwk= X-Received: by 2002:a02:c04a:: with SMTP id u10mr19869616jam.59.1554699918552; Sun, 07 Apr 2019 22:05:18 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 8 Apr 2019 02:05:07 -0300 Message-ID: To: Dan Ackroyd Cc: Guilliam Xavier , PHP internals Content-Type: multipart/alternative; boundary="00000000000060b0be0585fdc878" Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: david.proweb@gmail.com (David Rodrigues) --00000000000060b0be0585fdc878 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Real world example: class Paginator { // If null, will use the default value (eg. 15), else, it will requires an int. public function setItemsPerPage(?int $itemsPerPage) { ... } } // Info that came from user input. // It will be a string, and not a integer, like we need. $itemsPerPage =3D Input::get('itemsPerPage'); // If '' then $itemsPerPage =3D string '' // If '123' then $itemsPerPage =3D string '123' $paginator->setItemsPerPage($itemsPerPage); // Error: expected int, got string. Current solution: $itemsPerPage =3D Input::get('itemsPerPage') ?: null; $itemsPerPage =3D $itemsPerPage !=3D=3D null ? (int) $itemsPerPage : null; $paginator->setItemsPerPage($itemsPerPage); // OK With this new feature: just... $paginator->setItemsPerPage((?int) Input::get('itemsPerPage')); // OK Question: why just not check if $itemsPerPage is set like: $itemsPerPage =3D Input::get('itemsPerPage'); if ($itemsPerPage) { $paginator->setItemsPerPage((int) $itemsPerPage); // OK } Answer: because in this example we could do that (although the new solution is much more practical). In another case, I have a static factory method that depends of an ?int to be created, and I can't just skip it with an if() if user input is empty. And about "settype($variable, "?int")" it was requested on original mailing by Claude Pache. Em dom, 7 de abr de 2019 =C3=A0s 23:54, Dan Ackroyd escreveu: > 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 > --=20 David Rodrigues --00000000000060b0be0585fdc878--