Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114638 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95584 invoked from network); 27 May 2021 13:11:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 May 2021 13:11:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4347D1804B5 for ; Thu, 27 May 2021 06:23:13 -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=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, NICE_REPLY_A,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-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 ; Thu, 27 May 2021 06:23:12 -0700 (PDT) Received: by mail-wr1-f50.google.com with SMTP id x8so4742418wrq.9 for ; Thu, 27 May 2021 06:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=VGn6e7xd5BWiJIq1AkK/xFfsixEZvuazDWu7bShA58U=; b=aKbAoLWXQcSADye3T0zjyr0Jeiyuq28vsXTONhQvevA6LlG4yMMQanl4R8QnEgLnLa 8iQ8CGrYVu/WwVJI4qwIsxZBw2oxWdSDywZ+MOOLVKMRbwKHiYwBVU6YqDKTIMGqh9aA ygh44Q+8JlA1LWYS/q9eCP4YJvnKwxQH7oKFdoS3lAiGSuQCPJ+vxIO3tKPnnAelC8IE 1Dxn7pWAyjUvnRPmLoxUfAYD3UgwRlJdKjsYagKG1E5d9QObRNLxNKW4uFisOPYe565F KRnJ+TubWRUf6TUVg95msqoLcJIcuLNr4ZMdsSP5DkDpe6CyotgCSVzGePiWJEB0oWpD vYBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=VGn6e7xd5BWiJIq1AkK/xFfsixEZvuazDWu7bShA58U=; b=J978ErXN/V6gVJV3R5bGO61zr5Njmf0LT2EF1CrQsqHafJA60mw6HS53SIXnHSjF21 T16KZ01Xk5ZD1THRH0cw3jprViMjx+HzIejrbz2s26gZzvEQfboRGypw7WZEEzVw26pG GaW2jXLvplOsze0YES7zajrPxnq81enWpmfiCWkxAgi95objCCfosluXZHem9rxFCr6Z T0Hc4xNyBlJ5eSummqo/BGa3diM2NPS1LnHrwpxeJUNN5BVjjXLG61ahRfl0KYGS3r/i VGZP+o9jM833xeZyrA8Z8MRWl2uJX0AFvyWl0dJcokJAk4XH+U5RJ5bNO64zjpXk7qp2 Zj9Q== X-Gm-Message-State: AOAM533MWPuRh4H1uaUTdpMwcZ+68xlvUS3CegPYRBBsqN5nTPz1VY/D GyUY03FJOhdcYiolTIzZ/wMpFrWmHvI= X-Google-Smtp-Source: ABdhPJzvq0d+/0fDPWS/SjYGpD0rekY7TjCQtozZGXUMQdVKHgIIxLvPjC3BBSybJ2fN7zPUmzmXBw== X-Received: by 2002:adf:ed0d:: with SMTP id a13mr3267017wro.371.1622121791609; Thu, 27 May 2021 06:23:11 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id r2sm3497443wrv.39.2021.05.27.06.23.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 May 2021 06:23:11 -0700 (PDT) To: internals@lists.php.net References: <80fb5a8e-d770-49fd-45d5-ea6f4b004513@gmail.com> <2874cf7c-e55d-4100-bb01-af11a5ab6e87@www.fastmail.com> <93bee614-8bca-7e35-94a6-518beafac8ba@processus.org> Message-ID: <0936e748-c0af-4b4e-7bb3-808d399811c7@gmail.com> Date: Thu, 27 May 2021 14:23:10 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Consensus Gathering: is_initialized From: rowan.collins@gmail.com (Rowan Tommins) On 27/05/2021 13:07, Pierre wrote: > So, if it's going to be used and abused in those magic APIs we all use > daily without thinking about it (let's say, hydrators or serializers, > dependency injection containers and all that stuff, and you do > maintain some if I recall) and unlock some performance optimizations > in there, I'd say go for it. I think "abused" is the right word here. The uninitialized state exists in the first place because we don't have a way to enforce initialization in constructors, not because we really wanted a new special value that is "like null, but even more so". The combination of union types and enums actually gives a much more expressive way of representing "valid value or explicit null or special default". To use the ORM lazy-loading example: enum ORMState {     case Unloaded; } class Example {     private int|ORMState|null $foo = ORMState::Unloaded;     public function getFoo(): ?int {         if ( $this->foo === ORMState::Unloaded ) {             $this->foo = $this->loadFromDatabase('foo');         }         return $this->foo;     }     private function loadFromDatabase($fieldName): ?int {         echo "Fetching '$fieldName' from database...\n";         return 42;     } } // Create object; note there's no constructor, but the property has a default so is never uninitialized $obj = new Example; // On first call to method, the default value is detected and the property lazy-loaded var_dump($obj->getFoo()); // Subsequent accesses return the loaded value directly, at the cost of a single strict comparison, no magic function needed var_dump($obj->getFoo()); Unlike abusing "uninitialized" as a third state (next to int and null), this allows any number of states to be recorded, and treated appropriately. For instance, an auto-increment key might default to ORMState::NotYetCalculated, and trigger an error rather than a lazy-load. Regards, -- Rowan Tommins [IMSoP]