Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88420 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84503 invoked from network); 22 Sep 2015 19:28:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Sep 2015 19:28:52 -0000 Authentication-Results: pb1.pair.com header.from=jgmdev@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jgmdev@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.176 as permitted sender) X-PHP-List-Original-Sender: jgmdev@gmail.com X-Host-Fingerprint: 209.85.160.176 mail-yk0-f176.google.com Received: from [209.85.160.176] ([209.85.160.176:33605] helo=mail-yk0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 83/A2-00408-4FBA1065 for ; Tue, 22 Sep 2015 15:28:52 -0400 Received: by ykft14 with SMTP id t14so20213406ykf.0 for ; Tue, 22 Sep 2015 12:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-type:content-transfer-encoding; bh=LvUS16vMLGCa0g3pFZjBbMpKCREKS654YVnC9SpLhGo=; b=yguyWfRfxZVAUsfWAX6GNCH4+ZNJPh2STq+0ZKpDObsq3iSa1VpQHhYtVCugtPEsrj PRChfRbQA6yah50ZbQEmJLOkVdUFcZuMhSPdRGau2K3JnfZ60Hj1I97DtRs6DLCjd3JH Eud7Gi6Wfh27WM5cEv1jOUzvXgY5ovNm6g6irIcwd+6HrpNLXaiEl17FexrdKrQxqi8r 3LCdZZHaNJMjqXbGP27yxjmKBF+cTPL5jsC6rUgzhsHzB+IJOBoo9LN5NMYK0qqxvoZ6 8vPoV0eu4/DGr8UHZbAuYSJYSy5Qiw4aupsWumZu4pwc48yGHU393qzNTql3sU0MrlZo jmzQ== X-Received: by 10.13.219.151 with SMTP id d145mr23072863ywe.77.1442950128420; Tue, 22 Sep 2015 12:28:48 -0700 (PDT) Received: from [192.168.1.121] ([24.41.170.168]) by smtp.googlemail.com with ESMTPSA id y187sm1839267ywe.23.2015.09.22.12.28.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Sep 2015 12:28:47 -0700 (PDT) To: Stig Bakken , Rowan Collins References: <55FB4791.4040807@gmail.com> <55FB4DFC.2060204@domain.invalid> <55FB555C.9070701@gmail.com> Cc: internals@lists.php.net Message-ID: <5601ABED.2080902@gmail.com> Date: Tue, 22 Sep 2015 15:28:45 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: Let's discuss enums! From: jgmdev@gmail.com (Jefferson Gonzalez) On 09/22/2015 06:38 AM, Stig Bakken wrote: > Actually, you need to think about compatibility in a bigger perspective. > > One of the first things I would want do if PHP were to grow enums, is > to add support for it to Apache Thrift. For those not familiar with > it, Thrift is basically an IDL for specifying messages and services > (APIs) that generates code for a lot of languages, including PHP. Same > principle as Google's Protocol Buffers / grpc. > > Anyway, some of the point in using an IDL for your APIs is having a > graceful way to deal with changes over time, because you must always > deal with a certain amount of old clients or servers. > > Thrift and Protobuf's enums are represented as integers internally, > and that is what goes on the wire. If you change the name of an enum > field > in your IDL, you will get a source incompatibility, but that's > acceptable as long as you change the code. It only affects that single > client or server. What is not okay is to change the enum's integer > representation, because that breaks the protocol, and you can no > longer communicate with older clients or servers. > > The point I'm trying to make is that an enum's normalized > representation should be an integer, and that is also how it should be > serialized. It also happens to be how most other languages chose to > represent enums, and deviating from that will cause all kinds of pain > the minute you need to do type conversions between formats or > languages. > > - Stig > Is also worth noting that if you keep performance in mind, enums whith values represented as integers should be a much more performant/efficient implementation since it would require less cpu cycles and memory to build and read them. In the other side the RFC proposal is treating the enum values as objects which would require a bigger zval and more cpu cycles to construct them and read them. So basically from a performance perspective enums expressed as: class enum { const FIELD1=0; const FIELD2=1; } should go more easy on the engine than: class enum extends enumeration { public FIELD1 = new enum(value, name); public FIELD2 = new enum(value, name); ... } The latter would be bloated, which emulates the java way of doing things, one of the reasons why JVM consumes so much ram and cpu cycles for simple applications. Again, enumerations should be a nice way of grouping/categorizing values/flags for a specific use. Over-engineering the concept can lead to bloatness. Enums should offer the commodities that we don't have with: class MyConstants{ const SOME_CONST; const OTHER_CONST; } These should be: 1. dedicated syntax 2. type hinting with range checking 3. much better and concise documentation that clearly shows what type of constants/flags a parameter can receive Everything else besides that can be considered over-engineering and bloatness (I over used the 'bloatness' term :D).