Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119490 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47757 invoked from network); 8 Feb 2023 16:14:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2023 16:14:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8F72E180548 for ; Wed, 8 Feb 2023 08:14:21 -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_H3,RCVD_IN_MSPIKE_WL,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-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 08:14:21 -0800 (PST) Received: by mail-ej1-f42.google.com with SMTP id p9so6459456ejj.1 for ; Wed, 08 Feb 2023 08:14:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=CniVUVT8mdo7GqL1B4gwwEZ8svxz7wdeAtzHkAAWUgg=; b=hP8HsW2+ycfCxCyqlKH1DlBEZiclTCrWScFLnwk/K3R2fLDJ3FuBbYVzV8Lc98vTzI 941AQviIJG7tZrqcUQcW5SwbnGI8icShfBB8TZiWYDsvL2llzPQl6O/u50FIYbm5FuO/ wPpTDLIaD69x1JfbX5BrCFeYRfEPBdL+NmQqG0lFCrQ8N8q+jYnZySaMjmBe/zwiU+uL sgdd7VZLqq2pLZABihvLaRqEnpGdDY2IOunfTYYi2gEzxt7QYgxYDafEHQp0ru1QRfFN J1FZ+5njsnsn1IAt1b4+yCeSAfnNpclAGbpAA6cG+3AC6rDr+0cdtIaL/pf1L/5aA7OJ q9Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=CniVUVT8mdo7GqL1B4gwwEZ8svxz7wdeAtzHkAAWUgg=; b=1o2+QduNX88FK7poFVX09+wlTeODBnHmf0ky0ys9yJWYkLlNzLkdTj68zrequ+WZNq 7XH6Ok2EhMt3UPc9mBfNg3JONUf8H4YgJZ+NHWPGDw2xj7XrqPigagvHTSMUdwMJC6Dw zetqCckvXMojNPBkKHerJYJTKdyBViJ3vumjetd7gfJ02pP8rLhhMfyd70beTwwT7ulv NPjoFnWF6Xv0530kfJ9kFjFwswyD2b7H81OreQLv7usHXFq4vQAMty4RmYvNwRWaQ6bL q90ggTj+B6ov893hEpK9q+E03ozTUju0YuyJk9RuX7pzZ4oDzAcNdQtcYZl3HnmGsNgs WhBg== X-Gm-Message-State: AO0yUKU9KV3/i077Ty78b3blWVsm7oMKu0Dj0DLC6D4dUlU8UWmoUGHE ndmI5nt9SYUhCSTRt/Hb5hEVKntfRNd92BdqBWIvGnG2 X-Google-Smtp-Source: AK7set8vPu2F3TtH1yXObl8j9KGGuxEYJwQAOhM7+9svzRY3RF/Tjlxc1S+PWnyiziC4vdgucn25nO54TsBku5OyZfE= X-Received: by 2002:a17:906:4407:b0:85d:6569:ff36 with SMTP id x7-20020a170906440700b0085d6569ff36mr1945538ejo.171.1675872859105; Wed, 08 Feb 2023 08:14:19 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 8 Feb 2023 17:14:07 +0100 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000074ad6a05f4328eef" Subject: Re: [PHP-DEV] 'Uninitialized' creates a lot of cluttering From: flexjoly@gmail.com (Lydia de Jongh) --00000000000074ad6a05f4328eef Content-Type: text/plain; charset="UTF-8" Hi Kamil, Thanks for your reply. With regards to the automatic default, of course I can only agree! Indeed isset() should not be needed for declared properties! But because of the typed properties, you have to initialize them before you can access them. I think that is cluttering up the code. From a programmer's perspective, the property is declared and should be accessible. Even if it has no value. There should be no difference between typed and none-typed properties for this. Greetz, Lydia Op wo 8 feb. 2023 om 15:58 schreef Kamil Tekiela : > 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 > --00000000000074ad6a05f4328eef--