Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57971 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91352 invoked from network); 23 Feb 2012 02:22:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Feb 2012 02:22:26 -0000 Authentication-Results: pb1.pair.com header.from=samuel.deal@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=samuel.deal@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: samuel.deal@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qw0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:40432] helo=mail-qw0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E8/80-07254-1E2A54F4 for ; Wed, 22 Feb 2012 21:22:25 -0500 Received: by qadc14 with SMTP id c14so1026367qad.8 for ; Wed, 22 Feb 2012 18:22:22 -0800 (PST) Received-SPF: pass (google.com: domain of samuel.deal@gmail.com designates 10.229.77.131 as permitted sender) client-ip=10.229.77.131; Authentication-Results: mr.google.com; spf=pass (google.com: domain of samuel.deal@gmail.com designates 10.229.77.131 as permitted sender) smtp.mail=samuel.deal@gmail.com; dkim=pass header.i=samuel.deal@gmail.com Received: from mr.google.com ([10.229.77.131]) by 10.229.77.131 with SMTP id g3mr24870448qck.147.1329963742856 (num_hops = 1); Wed, 22 Feb 2012 18:22:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=tMcSi9lWKuU3i2/YFYbh8SxZspXe/TJvGS0hl8nd/6M=; b=c5MNXXjTkAccRCNb2dJA9xicMVfPF9YVK0iRUeA0pnWOEEhevShhxIRPTO6BFY72m1 TuFR73ze1wPcX83kE0igOjYLUNHD4v8IOcE4ewUb3SQR+hBCdjlZangWy/Z8397zaYoj J9VJtFagRz6BgIKDKP3/QKavPyxzoexGXqzEE= MIME-Version: 1.0 Received: by 10.229.77.131 with SMTP id g3mr21035766qck.147.1329963742686; Wed, 22 Feb 2012 18:22:22 -0800 (PST) Received: by 10.229.220.84 with HTTP; Wed, 22 Feb 2012 18:22:22 -0800 (PST) In-Reply-To: <4F457597.3060405@sugarcrm.com> References: <4F455C96.50706@gmail.com> <4F455E91.2060408@googlemail.com> <4F4570D6.8040003@gmail.com> <4F457597.3060405@sugarcrm.com> Date: Thu, 23 Feb 2012 03:22:22 +0100 Message-ID: To: PHP internals list Content-Type: multipart/alternative; boundary=002354470bfc7270b704b998505a Subject: Re: [PHP-DEV] [RFC] Enum proposal (yet another) From: samuel.deal@gmail.com (Samuel Deal) --002354470bfc7270b704b998505a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi ! First, thanks for all your replies. I'll try to answer all questions, it'll be long. #1. Should PHP have an enum implementation (not especially mine) ? I think yes. We do not require it, there's nothing we can't do without. However I think we need an intuitive and expressive syntax for this common concept, a kind of syntax sugar, like "++" operator. There's a lot of (beginners) php devs who tried to express this concept with serialized constants or other ugly practices. I don't say we must provide a language native structure for everything those devs make bad, they need to learn. Just, any devs I know, who came to PHP from another language, soon thought "How can I emulate enums" We do not have to copy other language; if someone has an expressive syntax for a more advanced concept, I'm in. Pros and cons: + expressiveness + performance for enum built-in functions - another keyword - (very very) little engine slowdown - work to do (but it could be a small hack, easy to maintains, and I'm ok do it) #2. Enum values should be objects or scalars ? I choose scalars for several reasons: - more in "=C3=A0 la PHP" style, not strictly typed (but we can provide bui= lt-in functions to do checks) - it's easier to communicate (to save into db, to retrieve from http post queries, etc...) - serialization is faster an shorter - it's easier to migrate from existing PHP code based on class constant to enum. - less memory consuming I didn't see real drawbacks of using scalars instead of objects. Except if you wanna have a really really strict typing. ( By strict typing I mean something like that: myEnum::foo =3D=3D 1 // =3D> false myEnum::foo =3D=3D myEnum(1) // =3D> true ) #3. Enum values: int or string ? Do we need an explicit values syntax ? I chose int, because: Most of time, in our code, we would write enum names ("myEnum::foo"). Code would be expressive, with a shorter memory footprint. You don't have to provide any value if you didn't need it. Less often you'll need string representation, but you can easily get it (in foreach or with enum built-in functions, myEnum::name($value) in my proposal) In addition, sometime you have to deal with other programs which required numeric values. In that case you can use explicit numeric values without writing a mapping function? Issues happened when you have to change the enum declaration, and have somewhere (database, session, etc...) values stored you can't easily change= . If it's INT-based: when you add or remove a value, you have to be careful not to change auto-incremented values. That's why providing an explicit value syntax is useful. If it's STRING-based: when a functionality is bad-named ("We agree about this, I won't change it" -- the client --), you're stuck with this old name all over your code forever. #4. What's wrong with class constants ? Nothing. My proposal is only a syntactic sugar to declare, iterate and manipulate a kind of class constants. #5. What about SplEnum ? I think it's a good alternative. If finally my RFC is rejected, I hope this will be include in official php version. But it's a strictly typed approach, and I prefer the other one (see #2) #6. Why didn't you push this RFC on the wiki. First, I had a lot of respect for work done, and I didn't want to override previous RFC Then, I'm new here, and there's no explanations in that situation in the RFC about RFCs. Finally, I don't have rights for that. Tell me what you want me to do about this. I hope I haven't forgotten a question. Thanks for reading this long post. Thanks for arguing. Regards --=20 Samuel DEAL samuel.deal@gmail.com --002354470bfc7270b704b998505a--