Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112179 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94316 invoked from network); 4 Nov 2020 05:39:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Nov 2020 05:39:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 386A21804D8 for ; Tue, 3 Nov 2020 20:59:48 -0800 (PST) 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_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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, 3 Nov 2020 20:59:47 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id l2so25448024lfk.0 for ; Tue, 03 Nov 2020 20:59:47 -0800 (PST) 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=WmUTfwq77/u4VyZZzZ8/pAPSAdlFQOPIaMs7BMurC9w=; b=XviApc3B+ws0+0DAxDYhkaODfst2KtpL0m6IPHA7TqaYaMh3CPDvxCQDneCXMX/s/c QvChd2CbU2fSPLljvQvE1WLYMQqWjT+C90bfvP66RKcFQNeNbfbEG3dZCL4jH6OhHQS2 SPjiH5RIZ96rmlb9SnIu+soFS114EbsnF5MK4/dQT6Mo1K7naLR9fMKsLcUTvYthqMs+ CvL5+xvNLcseHGtSMOGVsWRyqe1teJ/dpVH6lJJyFFY7p3aROYjenZRyFudz8NBz8FIY VWIxvVu8qCvVj26qKVYaMKicjBRNfV47b6rfwJGE5t+oUQgMLlmxihzBW7pyiK73i2fU FxGg== 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=WmUTfwq77/u4VyZZzZ8/pAPSAdlFQOPIaMs7BMurC9w=; b=tUi3GNpMQhJ0vFk1qjYGT1IE9o5DiHtpIJb6vgbCjV12zEt43GqaeGVw4Y9cJ6jglK N2KQbpb+H0NzGbSzB/CbkhpopVb8dUrguWOytjoZENqlTAYjybU+scZpdaHiYoQ8U+Kk itZZ8SY8csSU97jisViNjAphNCJYDWWlPcXtziXxNb+hwcJi0EdgLkS+rrA7s6kP3RvU U/4wZyXBOPzRDTcfFFHwICobFB5B5ewMUbpKsxVL7TI2LsAgNAQqxP3ey9hQ7R7PV3lq X0D8cFCjw6LOKElG5oJnWhkjvLAD4qVFiba5WzlXCAF9eTMPZfUBs23JMT8QBuPnvoHH U+Zw== X-Gm-Message-State: AOAM530BP8XOKA2rX6ubhqPiMX8pFfNP/0U9v6OetO5ccyhyAtrBUmES phyCALhuDiAeGiv3v8q9I2MnQvSx7XEgOp3G4fY= X-Google-Smtp-Source: ABdhPJwADRm7pXTXlPx++iEQXJjCXm0DSwIXk3pdrkUvbYzFBAURhISCdg0oa4iJbjkr6+FXVm7ucUS6Ry6J3UFD0vQ= X-Received: by 2002:a19:c70f:: with SMTP id x15mr8348706lff.296.1604465985883; Tue, 03 Nov 2020 20:59:45 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 4 Nov 2020 06:59:34 +0200 Message-ID: To: Marco Pivetta Cc: PHP Internals List Content-Type: multipart/alternative; boundary="0000000000002491a105b340d963" Subject: Re: [PHP-DEV] Nullsafe From: zsidelnik@gmail.com (Eugene Sidelnyk) --0000000000002491a105b340d963 Content-Type: text/plain; charset="UTF-8" But wait! In your example, funds won't get detracted. If `$accounts->get($receiver)` will return `null`, then everything inside `addFunds(...)` will not be executed. Your example (simplified): https://3v4l.org/38Dk3 Another one: ```php function expensive_function() { var_dump(__FUNCTION__); } $foo = null; $foo?->baz(expensive_function()); ``` On Tue, Nov 3, 2020 at 10:11 PM Marco Pivetta wrote: > Heya, > > On Tue, Nov 3, 2020, 17:38 Eugene Sidelnyk wrote: > >> Hello, internals! >> I am wondering why don't we use ordinary `->` operator with safe null >> handling? Programmers will be more prone to return null values. And thus, >> in most of cases `?->` will replace `->`. >> Why do we need another operator, if we can implement null safe in current >> operator without BC breaks? >> > > Overall, "null safe" can be very dangerous if made the default. > > Here's a scenario where I'd never want "null safe" behaviour (which does > anything but introducing safety): > > ```php > $accounts->get($receiver) > ->addFunds( > $accounts->get($sender) > ->detractFunds($amount) > ); > ``` > > In the above scenario, if the first `$accounts->get()` call returns `null` > for any reason, you may actually destroy money (funds detracted, but never > added to another account). > > The example is simplistic, but it shows that "null safe" is everything but > "safe", and it must instead be used only where it absolutely makes sense to > suppress null reference errors. > > Similar behaviour can be observed around cast operators, which are too lax > for most businesses logic: > https://github.com/ShittySoft/symfony-live-berlin-2018-doctrine-tutorial/pull/3#issuecomment-460441229 > > Safe = crashes when it should crash. > >> --0000000000002491a105b340d963--