Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112427 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97257 invoked from network); 5 Dec 2020 15:39:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Dec 2020 15:39:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 087AD180531 for ; Sat, 5 Dec 2020 07:08:26 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 5 Dec 2020 07:08:25 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 72C0A78C for ; Sat, 5 Dec 2020 10:08:24 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Sat, 05 Dec 2020 10:08:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=I/pcq5 76Y1MQNeF7PAph7B9Wo8qhiXkELH22e8Jarvw=; b=D4v+IZOAiLyEI2FcoWhu1T LsdcDMcI4650NJ/sIk+Tjge9O6UmOQ32iSDNuTA1C+sRhTplQ05oRWdFmPUsU2cm 1usv+FJZJG4lbGRf1I3lQT/Jg1EiTQQN0aeoe+ev7wydMVo/fXfs8RolrxdUoxiZ /Btk3lJuVCDwF/DAnKGoYyx6gNt0H1hmzC8xp3MMg9wsfwd4tFAlUgBDMrt3f55i 1DzVqvW89USfB9+9mDxkDnNc3XIZe4atxdO+Q1q4KCvLzQxVJR9OfO+EG+HG339u w0AqqFoowOii3byj+A3Z7YrWvhvUmdDkXwjPHqTNAIRybrLjWTIvaU0PqjaQmI4Q == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudejtddgjeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 63A5D14200A2; Sat, 5 Dec 2020 10:08:23 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-622-g4a97c0b-fm-20201115.001-g4a97c0b3 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Sat, 05 Dec 2020 09:08:02 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Enumerations From: larry@garfieldtech.com ("Larry Garfield") On Sat, Dec 5, 2020, at 8:21 AM, Marc Bennewitz wrote: > *dons flame-retardant suit* > > Hi Larry, > > thanks for your great initiative and hard work in this! > I'm the author of an emulated enumeration lib [1] and really looking > forward seeing native enumeration support in PHP. > > Here are some questions about your RFC: > > * How can you access a defined case value? > Something like `Suit::Spades->value()` At the moment it's only accessible via the ::cases() method. It may be appropriate to yoink the ->value() or ->value name to access it another way. We're debating that right now. The primitive equivalent case is tricky, because other languages have about 14 different ways to think about it, all of them in the end incompatible. :-) We know we don't want to just have "fancy constants," but for those cases you do want/need a primitive instead (mainly writing to a DB or screen) it needs to be easy enough to get that. There's a number of competing priorities we're trying to balance here to make the DX as clean as possible. To also respond to Pierre here, at present the primitives must be unique. We want to minimize the boilerplate that people have to write on every enum for common cases, and the cases() method is part of that. It won't capture everything, obviously, but if the common cases can be made trivial and the rare cases possible, that's a win. > * Are the cases case-insensitive or case-sensitive? > `Suit::Spades` vs. `Suit::SPADES` They end up as class names internally, so I believe that means they'd be case insensitive. > * Are cases serializable? > `Suit::Spades === unserialize(serialize(Suit::Spades)) // true` Right now they'd do the same as objects, so they'd serialize as an object. Unserializing like that, though... hm, that would probably NOT still be === due to the way PHP handles objects. That's probably undesireable, but I'm not sure at the moment the best way around that. I'll have to discuss with Iliya. > * Do cases have a stable ordinal number / position and is that > accessible? > This would be very interesting on implementing an optimized EnumSet > using bit-array / bit-set internally The order returned from cases() is stable as lexical order, but there's no ordinal number to access. If you want a bit set, you could do: enum Permissions: int { case Read = 0x1, case Write = 0x10, case Exec = 0x100, } Right now there's no support for case ReadWrite = self::Read | self::Write. I don't know if that would be easy to add in the future, but I'd be OK with it if so. Mainly this runs into the same tricky questions around primitive equivalent handling (and what we can coax the lexer to do). > * I often use metadata in enumerations and so I would be very > interested to allow constants. > I do understand that they share the same naming table and these needs > to be unique but disabling constants altogether would limit the > use-cases in my opinion. That's what methods are for, or potentially __get. Allowing even > * Is it possible to detect / type-hint if something is any enum? > ` Suit::Spades instanceof Enum` > > This basically means that all enumerations would to based on a > general enum. > This would be very helpful on providing functionalities especially > for enumerations thinking about a doctrine enumeration type or again an > EnumSet / EnumMap. Not at the moment. We're discussing the implications of adding that. What exactly are EnumSet and EnumMap, in your mind? --Larry Garfield