Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105153 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93619 invoked from network); 8 Apr 2019 20:49:55 -0000 Received: from unknown (HELO outbound2.mail.transip.nl) (149.210.149.73) by pb1.pair.com with SMTP; 8 Apr 2019 20:49:55 -0000 Received: from submission15.mail.transip.nl (submission15.mail.transip.nl [149.210.149.152]) by outbound2.mail.transip.nl (Postfix) with ESMTP id 44dHsm4J8hzYdWT for ; Mon, 8 Apr 2019 19:46:36 +0200 (CEST) Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) by submission15.mail.transip.nl (Postfix) with ESMTPA id 44dHsl0jKQz3x6T for ; Mon, 8 Apr 2019 19:46:32 +0200 (CEST) Received: by mail-io1-f42.google.com with SMTP id v4so11812539ioj.5 for ; Mon, 08 Apr 2019 10:46:32 -0700 (PDT) X-Gm-Message-State: APjAAAWs6wMHD5EmEo4h0FRWUrKyeE1JFJL01OSC1kO952ql+JJBG0a2 BDqtMujp6BNrs9JmTAyi91hGK+KxjGz9SYa9DKo= X-Google-Smtp-Source: APXvYqxLBBkCfUD16APZfxjWN5ZpggrG2NGFn0ioyG+AkijN6sRE+TwsNMIoUVjxjvetpQQy+yuc/Pxk3S1JpxVGFLk= X-Received: by 2002:a5d:940c:: with SMTP id v12mr20852398ion.32.1554745589815; Mon, 08 Apr 2019 10:46:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 8 Apr 2019 18:46:20 +0100 X-Gmail-Original-Message-ID: Message-ID: To: David Rodrigues Cc: Dan Ackroyd , Guilliam Xavier , PHP internals Content-Type: multipart/alternative; boundary="00000000000098e2c40586086ae7" X-Scanned-By: ClueGetter at submission15.mail.transip.nl DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=transip-a; d=pmmaga.net; t=1554745596; h=from:subject:to:cc: references:in-reply-to:date:mime-version:content-type; bh=IBrK+xiKAg6BiHz6CksZPXd8mc6/CkfUOvjfX/G/STA=; b=Pm7Vzw7/YOJRaXRVLSiFfj6Ofq/ZesFw14LYsSodaL34TRdmmkIdj/VrM/uKm3LzP6D3gT DwDkxyEscvR3kcmszdyXI04cBKpYtliWCsvri9gY/XHeY41JEFNi72fmTLkVWuyuzNnth5 FozgbjB2nQvAkop3wLSvnDAS3aOSWv4k5a1zQhiSnL8N+Se/rDUahrSlKNfB+mqbgOQJSH klsuxKpQnox4UC1XPhFu4Kv+d1QQlkVVJruIZk4rMtdY3PStg8wPAt+jGwUg+w57mV4WtP o8g5JikHkPrsnMt4yrutVmfHa/qP6Ueen0StKb4ItGCfQooI3v7j0Yijf19bJQ== X-Report-Abuse-To: abuse@transip.nl Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: mail@pmmaga.net (=?UTF-8?Q?Pedro_Magalh=C3=A3es?=) --00000000000098e2c40586086ae7 Content-Type: text/plain; charset="UTF-8" On Mon, Apr 8, 2019 at 6:05 AM David Rodrigues wrote: > Current solution: > > $itemsPerPage = Input::get('itemsPerPage') ?: null; > $itemsPerPage = $itemsPerPage !== 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 = 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. > Hi! I'm not a fan of the approach for a few reasons: - First and foremost, we would introduce a cast where you can't be sure of what you'll get back. I understand the use-case for when you want to pass something to a nullable parameter, but if you think about this cast in isolation, it hardly makes sense. - Second, maybe this is particular to your example, but your are forcing yourself to process something that the user didn't set. If your paginator already has a sane default(15) in a property, just don't call its setter when the user didn't ask you to. If you insist that you really want to save LoC, just assume that 0 means "use the default" and you can use the cast in all cases. Regards, Pedro --00000000000098e2c40586086ae7--