Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119486 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39706 invoked from network); 8 Feb 2023 14:58:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2023 14:58:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8664180506 for ; Wed, 8 Feb 2023 06:58:45 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 8 Feb 2023 06:58:45 -0800 (PST) Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-4c24993965eso237059017b3.12 for ; Wed, 08 Feb 2023 06:58:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UV/ubvjNOlphdRk8PTbde82b4utkopZf4IOmPrG28QE=; b=mO+OYI0RdEG2tt/JwzJrYzQRPjUgZbVnsERLKKpfUeVaIQfsmOLv5+8DXY78uupMvz 257BSWx3s/01XddFhIBjtzh7piUXQWioV7oTEo/D9YQB8KdntJBylNXqc92kPY3yqrK/ 7k76SWwvGj0KkE+K6ocxV4+S3JZtQyX4nMQryHr+4bwxAWygiOmljPqlYI0UBXD7pGjN fYKGuqeV41buZWqn0mp99Tk/kCNaRevf8DCabphXAgAlGxdVyRUtwQVAbBs1Lx+QBrjR tP6IFRsFMpzCq/tE4UB1COdr1KDYUv5wqrxYpwmj+g3i58WBci6cM7SHugU3Xph6MObY lASA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UV/ubvjNOlphdRk8PTbde82b4utkopZf4IOmPrG28QE=; b=eRJ/IMtOndBdvuCHHssl/7zSwyyAF943lfI8c0GDAGdrGUPgIxpekXEBTDHLt6pFUX EoNGD5BnBNJmu3a1rk2Pishi1l6cp8+0kqMhr206PvdaMllU4ge3kqkx0d9/H43tGaHk vvvJQW9KoB5lESbBoMirqJXBtBAiZeQC/NyU+pg+k831O29wEWmuPkcQ4d20Vnf9v6Jy v4FUdKZf2dZEK/xtjDSFsQqxGPE5uarLK0Wc58vegnGmPpYHPoLX4QZaWdoDSO1nzyDq g/upRIZ0PSjzS7WZXEelyB1NPSLRsD7gp4HE7KeG6kfSxDpMlXOQ970hWqA6SALURih3 Ilzg== X-Gm-Message-State: AO0yUKWsbSgkT+7dkPeOO0jOMbhY19dsZKQNB+h5reS5wH6FrEKckrvP ao9fxxIYTySOwMv0nGO48XEBz0fxlyeCqrl9rnI= X-Google-Smtp-Source: AK7set+cueqtpajeAwuyKexjIGS76Nnc3x+T80liXkuTzfIOz7tNWhub7ihdzBgMp2/d5TjGlfXewDbENoitRuh6F7Y= X-Received: by 2002:a0d:e209:0:b0:507:77a2:f50f with SMTP id l9-20020a0de209000000b0050777a2f50fmr919457ywe.420.1675868324915; Wed, 08 Feb 2023 06:58:44 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 8 Feb 2023 14:58:33 +0000 Message-ID: To: Lydia de Jongh Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000326e2d05f43180ea" Subject: Re: [PHP-DEV] 'Uninitialized' creates a lot of cluttering From: tekiela246@gmail.com (Kamil Tekiela) --000000000000326e2d05f43180ea Content-Type: text/plain; charset="UTF-8" Hi Lydia, I understand where you are coming from because I encountered this a few times myself, but every time it was actually because I was doing something wrong. The reason for this limitation is actually very simple and logical. Let me explain. When you use untyped properties, the type is not restricted so every property can be nullable by default. It's very simple for the language to have NULL as the default value for all untyped properties. When using typed properties, the language cannot use NULL as the default anymore because the type might not allow NULL, e.g public string $name allows only string values. Unless you assign some string value to it, the language will not initialize it with any value. If you need the property to have a state indicating no value, you can make it nullable and assign NULL as the default value. What you are proposing is actually not acceptable from the type system's point of view. We cannot add a new syntax for assigning default values to type properties. It might make sense in a very limited scope, e.g. public ! int $number; defaults to 0, but in any other more complex scenario, this fails miserably. Consider this example: class Test { public ! MyObject&SomeInterface $prop1; public ! int|false|null $prop2; } In the example above, what default values could be assigned to the properties? The correct answer is none. You cannot have a default object, nor can you pick a default value from the union of three types. There is simply no way to determine the default value. So if we had a new syntax like this, it would be severely limited to only the simplest scenarios. This would create a new inconsistency for the benefit of using a single character instead of assigning the value. There would be no benefit to introducing such inconsistency to the language. It is the job of the developer to specify the default type. Some things can be left for PHP to infer, but when it comes to the default property value, the developer needs to assign it. You said that an isset is necessary when using typed properties. I would argue that an isset is not needed. Using isset with typed properties is a code smell in many circumstances. If the property can be unassigned, the developer should use a sentinel value such as null to indicate this. If the property is always expected to hold a value, then the code should not allow a path to access it before it's initialized. Kind Regards, Kamil --000000000000326e2d05f43180ea--