Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88296 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75953 invoked from network); 17 Sep 2015 23:16:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2015 23:16:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=marcio.web2@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=marcio.web2@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.175 as permitted sender) X-PHP-List-Original-Sender: marcio.web2@gmail.com X-Host-Fingerprint: 209.85.160.175 mail-yk0-f175.google.com Received: from [209.85.160.175] ([209.85.160.175:35617] helo=mail-yk0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/E0-05737-BD94BF55 for ; Thu, 17 Sep 2015 19:16:44 -0400 Received: by ykdu9 with SMTP id u9so31880532ykd.2 for ; Thu, 17 Sep 2015 16:16:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=HUxxpHURgy6fve1L0btmHXpmC04vgCvG8EfZLmat/s0=; b=AT6o6QG5PLVf8LwvgCcWOYiJAQ7p1RGRMqb+smbIHIYVlJ8wT8Y6Poa3PrlyEiiLAb AJEidlt14G+CWXkGiJtSEsFy1fpw9qo7a8QaNJILIf8iBtCZJ5BSdrbK/eoQnMiS8X96 80h11sGUXq36AE5zDVnwKle4K5xpwg23EkI1D9CNtzoFd6qaKY16nqfj5jvqhcnfD1p6 gtIt8HfXTochwxVL/z8qiKvoWKmmpxG8vi39JdDS30CYf8nfWPwFD4t4IhO+yvZKXEHI 5mXgqk+E5SsMTeyaoD7HkoxtKzh+S+pxryegRVFaYYYELP3sVhIt9xJfyWXifkHDx+/E debA== X-Received: by 10.129.114.86 with SMTP id n83mr2055846ywc.43.1442531800997; Thu, 17 Sep 2015 16:16:40 -0700 (PDT) MIME-Version: 1.0 Received: by 10.13.197.195 with HTTP; Thu, 17 Sep 2015 16:16:21 -0700 (PDT) In-Reply-To: <55FB4791.4040807@gmail.com> References: <55FB4791.4040807@gmail.com> Date: Thu, 17 Sep 2015 20:16:21 -0300 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Let's discuss enums! From: marcio.web2@gmail.com (Marcio Almada) Hi, 2015-09-17 20:06 GMT-03:00 Rowan Collins : > Hi All, > > This has come up in passing a few times recently, but I'm not sure there'= s > ever been a dedicated discussion of it: would it be useful for PHP to hav= e a > built-in Enumeration type, and if so, how should it look? > > Many other languages have enum types, with varying functionality. The > central concept is always that there's some declared type with a bunch of > named constants, but beyond that there's a lot of variability: for starte= rs, > is it a type of object, a special way of naming integers, or an uncompara= ble > type more like booleans and nulls? > > > Here are my thoughts on what I'd like from a PHP enum - I'm not using > "should" in a particularly strong sense, it's just a way of framing the > points of discussion: > > 1) enums should act like values, not mutable objects; an array-style > copy-on-write behaviour would be possible, but it feels wrong to me to st= ore > any state in an enum variable, so just plain immutable would be my > preference > > 2) the members of an enum should have a name which is accesible as a stri= ng, > e.g. Weekdays::SUNDAY->getName() =3D=3D 'SUNDAY' > > 3) there should be no accessible "value" for a member; the value of > Weekdays::SUNDAY is simply Weekdays::SUNDAY, not 0 or 7 or 'SUNDAY' (I'm > thinking that internally, each member would be represented as an object > pointer, so there's no real benefit to forcing people to number everythin= g > like some languages do) > > 4) each enum member should be considered a singleton, in the sense that y= ou > can't construct or destroy one, only reference them; all possible instanc= es > would be created as soon as the enum was defined > > 5) following from (3) and (4), an enum value should compare equal only to > itself, unlike in C# for instance, where it would be comparable to an > integer based on its numeric value; similarly it shouldn't be possible to > cast to or from an enum > > 6) an enum should be able to have additional fields; these would be > initialised in the enum's definition; this is inspired by Java and Python= 's > ability to pass parameters to the "constructor" of the enum, but it feels > weird to me for any logic to happen in that constructor other than simple > assignments, so I'm thinking of a simpler syntax and implementation. It a= lso > simplifies immutability if no userland code ever writes to the properties= . > There may be an important use case for constructor logic I'm missing thou= gh? > > 7) an enum should have default static methods for accessing all the membe= rs > of the enum as an associative array > > 8) enums should be a first-class type, is_object(Weekdays::SUNDAY) should > return false, for instance; maybe Weekdays::SUNDAY instanceof Weekdays > should return true though > > 9) additional static and instance methods should be definable, bearing in > mind the immutability constraints already discussed > > > Given the above, I think we might end up with something like this: > > enum Weekdays { > member MONDAY; // if there are no fields to initalise, the member jus= t > needs its name declaring > member TUESDAY ( 2, 'Chewsdae' ); // positional arguments for populat= ing > fields in the order they are defined; a bit like Java, but without the > constructor > member WEDNESDAY { $dayNumber =3D 3, $sillyName =3D 'Wed Nose Day' };= // or > maybe a named-parameter syntax to make things clearer > member THURSDAY, FRIDAY, SATURDAY, SUNDAY; // don't force people to > write each entry on its own line; maybe even the "member" keyword is too > much? > > public $dayNumber, $sillyName; // fields initialised for each member > > public static function getWeekend() { > return [ self::SATURDAY, self::SUNDAY ]; > } > > public function getZeroIndexedDayNumber() { > return $this->dayNumber - 1; > } > } > > $today =3D Weekdays::THURSDAY; > foreach ( Weekdays::getMembers() as $day_name =3D> $day ) { > echo $day->dayNumber; > if ( $day =3D=3D $today ) { > echo "Today!"; > } else { > echo $day_name; // equivalently: echo $day->getName(); > } > } > > // Do we need a static method to access a member by name, or is this good > enough? > $favourite_day =3D Weekdays::getMembers()[ $_GET['fav_day'] ]; > > So, what are anyone's thoughts? Am I rambling on too much as usual? ;) > > Regards, > > -- > Rowan Collins > [IMSoP] > A kitten is working on that https://wiki.php.net/rfc/enum. Many points on the linked RFC are compatible to the points you raised so it might be worth reading before even starting any new proposal. Regards, M=C3=A1rcio