Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57938 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81436 invoked from network); 22 Feb 2012 08:45:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Feb 2012 08:45:59 -0000 Authentication-Results: pb1.pair.com smtp.mail=charlie@charliesomerville.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=charlie@charliesomerville.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain charliesomerville.com from 209.85.210.42 cause and error) X-PHP-List-Original-Sender: charlie@charliesomerville.com X-Host-Fingerprint: 209.85.210.42 mail-pz0-f42.google.com Received: from [209.85.210.42] ([209.85.210.42:42193] helo=mail-pz0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 52/F0-09421-04BA44F4 for ; Wed, 22 Feb 2012 03:45:56 -0500 Received: by dang27 with SMTP id g27so8963028dan.29 for ; Wed, 22 Feb 2012 00:45:46 -0800 (PST) Received-SPF: pass (google.com: domain of charlie@charliesomerville.com designates 10.68.234.71 as permitted sender) client-ip=10.68.234.71; Authentication-Results: mr.google.com; spf=pass (google.com: domain of charlie@charliesomerville.com designates 10.68.234.71 as permitted sender) smtp.mail=charlie@charliesomerville.com Received: from mr.google.com ([10.68.234.71]) by 10.68.234.71 with SMTP id uc7mr1340747pbc.97.1329900346199 (num_hops = 1); Wed, 22 Feb 2012 00:45:46 -0800 (PST) Received: by 10.68.234.71 with SMTP id uc7mr1107690pbc.97.1329900346109; Wed, 22 Feb 2012 00:45:46 -0800 (PST) Received: from charlie-mac.local (202-161-31-225.dyn.iinet.net.au. [202.161.31.225]) by mx.google.com with ESMTPS id vy2sm10756604pbb.48.2012.02.22.00.45.43 (version=SSLv3 cipher=OTHER); Wed, 22 Feb 2012 00:45:45 -0800 (PST) Date: Wed, 22 Feb 2012 19:45:40 +1100 To: Sebastian Krebs Cc: internals@lists.php.net Message-ID: <979071BE17F243D1B15F629F39023119@charliesomerville.com> In-Reply-To: References: X-Mailer: sparrow 1.4.2 (build 959.4) MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="4f44ab34_189a769b_393" X-Gm-Message-State: ALoCoQkndu5rxFVb2Dljufe3BVHhTJQ9tqVczEjwRxDygD8fGTccFG12Yjxvba4FHAyXsUxhZoO6 Subject: Re: [PHP-DEV] [RFC] Enum proposal (yet another) From: charlie@charliesomerville.com (Charlie Somerville) --4f44ab34_189a769b_393 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My point is that *although* it's certainly possible to do almost everythi= ng I want already, I should not have to write all that boilerplate for ea= ch enum. I would much prefer to write something like 'enum =7B A, B, C =7D' rather= than write a full class for the enum, duplicate each enum value multiple= times (your example repeats 'foo' and 'bar' three times each), overload = the =5F=5FtoString() magic method, do manual error handling in the constr= uctor, etc. Since PHP provides no way to overload the equality operator, any solution= that tries to emulate enums as Samuel or I have written about will be fl= awed. =20 On Wednesday, 22 =46ebruary 2012 at 7:37 PM, Sebastian Krebs wrote: > 2012/2/22 Charlie Somerville > =20 > > Right, but enums could possibly be a lot richer than class constants = are > > now. > > =20 > > They could be a type where the only values are what's defined in the = enum. > > This could be used with type hinting: > > =20 > > enum =46oo =7B > > A, > > B, > > C > > =7D > > =20 > > function bar(=46oo =24x) =7B > > // ... > > =7D > > =20 > =20 > =20 > =20 > class MyEnum =7B > const =46OO =3D 'foo'; > const BAR =3D 'bar'; > private =24value; > public function =5F=5Fconstruct (=24value) =7B > if (=21in=5Farray(=24value, array(self::=46OO, self::BAR)) throw new > UnexpectedValueException; > =24this->value =3D =24value; > =7D > public function =5F=5FtoString () =7B return =24this->value; =7D > =7D > =20 > =20 > function doSomething (MyEnum =24foo) =7B /* code */ =7D > =20 > =20 > What I wanted to say: I don't see, what is not possible already=3F > =20 > =20 > =20 > > =20 > > There'd be no need to do any manual checking that =24x is a valid val= ue. > > =20 > > Perhaps enum values could also be casted back to strings: > > =20 > > (string)=46oo::B; // =22B=22 > > =20 > > Sure, this is the kind of stuff that's possible in other ways already= , but > > first class support always helps. > > On =46eb 22, 2012 4:14 PM, =22Laruence=22 wrote: > > =20 > > > On Wed, =46eb 22, 2012 at 12:45 PM, Samuel Deal > > > wrote: > > > > Hi all, > > > > =20 > > > > I really missed enums in PHP, > > > Why=3F we have class constant. > > > =20 > > > thanks > > > > So after reading I tryed to synthetise the various views. > > > > =20 > > > > You can find a draft here : > > > > https://github.com/SamNrique/php-src/wiki/R=46C-draft > > > > (I can't write on the wiki, and perhaps it's better to finish the= > > > > discussion first) > > > > =20 > > > > There's an implementation with this draft. > > > > I'd love to have feedbacks because it's my first php-core hack. > > > > =20 > > > > Thanks > > > > -- > > > > Samuel D=C3=A9al > > > > samuel.deal=40gmail.com (mailto:samuel.deal=40gmail.com) > > > > =20 > > > =20 > > > =20 > > > =20 > > > =20 > > > -- > > > Laruence Xinchen Hui > > > http://www.laruence.com/ > > > =20 > > > -- > > > PHP Internals - PHP Runtime Development Mailing List > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > =20 > > =20 > > =20 > =20 > =20 > =20 --4f44ab34_189a769b_393--