Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117447 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15129 invoked from network); 30 Mar 2022 01:34:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Mar 2022 01:34:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8AA46180382 for ; Tue, 29 Mar 2022 20:03:00 -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=-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, 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-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (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, 29 Mar 2022 20:03:00 -0700 (PDT) Received: by mail-ej1-f48.google.com with SMTP id c10so20386945ejs.13 for ; Tue, 29 Mar 2022 20:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/p7nrIJTRejKceBHAYGuk3Bcw8K3E54VhuVA0Q9Lz7g=; b=gHH6GmgyRQTehsEwHwYKEl0J8g0cpqgrU8qR4ZIYyFS0fjQyU5gU3zTy8uw7cnw6M6 if17aZytjhdyqte8f4VG6UdrXCIs6r/koiVQDUHgukD2/UTvSB/M6A9LJLdF3gasANdS Q4B67s5LEcpjww91fWeLQWhQB5C43IGhCASxMhp/LdKnpvHX+VxUYWwURvdBovvw8Rtr HTQmeuihpcXqFApV5Xldw5Jnd1qmmCAgO4BlfeRvYh2/KCisjtzfpAPDdbFXAyJMqdnY zwcLOMWO48XvZuuWm8F2Dl2UaG0EaLQcigVCjMmS0mOU9uvBj8e12wD9bvHLH7+V173t iPsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/p7nrIJTRejKceBHAYGuk3Bcw8K3E54VhuVA0Q9Lz7g=; b=whZzSj6KiQuejHkWyEGV1jDAt673P+ZNnLGLiVm286iLcF339G2XjkQ3UEvQgiebn5 IXyeoJkdLe3t+hlJplNtGjX5Dsizeb5rziOt7nuZkLWdwydc6hKcfBx9ytPu/uX1Hwh1 txBIbEEwv3H6yL4aOJdDQH9y3r6v38imCwOxwzQmxFY7BOGjtMFUsg5hwgiDkYN023EI wc6AyyiGXU3jddxL5HqFIp+FA30ovlAsm7/vO+r/fNerBZHi3cOpP+wKPXjW5aaHIkHN /t9/7wRAaO3J3XyBn/j5De8O0bJpRcjTzmLuGSFhDQSd9arYQcxh9cTImddy7/s/T0nA EKTw== X-Gm-Message-State: AOAM531nXVkBdU+WOXrJZRWEUjmYQX25xoYUH1qkGklOqTb60hdKJkk6 GA8xaNMMPZVpaxiaXhg6oTwYzcnKucNrdSVpIcmC7dw48P8= X-Google-Smtp-Source: ABdhPJynOMFI/b7g3DlIbvM/RUIbbQYb+OF3a72wIVbbo/4oDv5QY0/jixNugvG6EcD2y7VbL3kMRq4FHT6T2Oe7lKU= X-Received: by 2002:a17:906:7944:b0:6da:b834:2f3e with SMTP id l4-20020a170906794400b006dab8342f3emr38835066ejo.353.1648609378598; Tue, 29 Mar 2022 20:02:58 -0700 (PDT) MIME-Version: 1.0 References: <76c399cb-fb29-4583-a212-8eb69740c96b@www.fastmail.com> In-Reply-To: Date: Tue, 29 Mar 2022 21:02:47 -0600 Message-ID: To: Guilliam Xavier Cc: php internals Content-Type: multipart/alternative; boundary="0000000000006282e205db66c8b5" Subject: Re: [PHP-DEV] Typed constants revisited From: mbniebergall@gmail.com (Mark Niebergall) --0000000000006282e205db66c8b5 Content-Type: text/plain; charset="UTF-8" On Mon, Mar 28, 2022 at 6:54 AM Guilliam Xavier wrote: > Constants are not abstract in an interface - they must be assigned a >> value. Only classes and methods can be abstract. Within an abstract class >> it is not valid to have an abstract property. Properties can be defined >> `protected int $id;` and optionally assigned a value `protected int $id = >> 5;`, but cannot be `abstract protected int $id;`. >> > > That's the *current* state of the language indeed, but to me, [part of] > your proposal looks like introducing "abstract constants"... Maybe I'm > misunderstanding? > > >> So to me it makes more sense to have constants follow the same syntax as >> properties `public const bool CAN_FLY;` without the `abstract` keyword. >> >> An example: >> >> ``` >> abstract class Bird >> { >> public const bool CAN_FLY; >> protected bool $isExtinct; >> ``` >> >> This allows for similar behavior, similar requirements, and similar >> syntax - consistency ftw! >> > > For similarity/consistency, the `$isExtinct` property should probably be > [`public` and] `static` (actually `readonly` too, but cannot be both). > > But, again, we can also see some similarity with `static` *methods*, e.g.: > > ``` > abstract class Bird > { > abstract public const bool CAN_FLY; > abstract public static function isExtinct(): bool; > } > class Dove extends Bird > { > // NOTE: the following two lines are required (in the class definition) > public const bool CAN_FLY = true; > public function static isExtinct(): bool { return false; } > } > // var_dump(Dove::CAN_FLY); > // var_dump(Dove::isExtinct()); > ``` > > Besides, an uninitialized property must be initialized before first read, > but is *not* required to be overridden/redefined (with a value) in a child > class; so in this example (still hypothetical): > > ``` > abstract class Bird > { > public const bool CAN_FLY; > public static bool $isExtinct; > } > class Dodo extends Bird > { > // NOTE: the following two lines are commented out > // public const bool CAN_FLY = false; > // public static bool $isExtinct = true; > } > var_dump(Dodo::CAN_FLY); > var_dump(Dodo::$isExtinct); > ``` > > where would the [missing value for constant] error be: on the definition > of class Dodo (like for an unimplemented abstract method), or only when > trying to access Dodo::CAN_FLY (like for an uninitialized property)? > > >> >> There seems to be interest and good use cases (thanks Sara for the good >> practical example!). At this point I'm going to work on a new RFC with all >> the details and feedback from this discussion. >> >> > > Thanks & good luck! =) > I have updated the RFC https://wiki.php.net/rfc/typed_class_constants with more details and examples from this thread, and updated the RFC status to Under Discussion. Hopefully the updated RFC helps answer questions and clarifies what the proposal includes. Of note is the "Inheritance and variance" section, which details uses with abstracts and interfaces, plus the "Constant values" section which includes details about errors. > > -- > Guilliam Xavier > --0000000000006282e205db66c8b5--