Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88301 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85520 invoked from network); 18 Sep 2015 00:09:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 00:09:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.109 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.109 blu004-omc2s34.hotmail.com Received: from [65.55.111.109] ([65.55.111.109:59065] helo=BLU004-OMC2S34.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 80/00-19961-5265BF55 for ; Thu, 17 Sep 2015 20:09:10 -0400 Received: from BLU436-SMTP62 ([65.55.111.71]) by BLU004-OMC2S34.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Thu, 17 Sep 2015 17:06:55 -0700 X-TMN: [EsWvU1UGzqMPR5Zb3Jse/z2OkRw3bhz+] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) In-Reply-To: Date: Fri, 18 Sep 2015 02:06:50 +0200 CC: Rowan Collins , internals@lists.php.net Content-Transfer-Encoding: quoted-printable References: <55FB4791.4040807@gmail.com> To: John Bafford X-Mailer: Apple Mail (2.2104) X-OriginalArrivalTime: 18 Sep 2015 00:06:53.0053 (UTC) FILETIME=[EC5362D0:01D0F1A5] Subject: Re: [PHP-DEV] Let's discuss enums! From: bobwei9@hotmail.com (Bob Weinand) > Am 18.09.2015 um 01:52 schrieb John Bafford : >=20 > On Sep 17, 2015, at 19:16, Bob Weinand wrote: >>=20 >>> Am 18.09.2015 um 01:06 schrieb Rowan Collins = : >>>=20 >>> 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 have a built-in Enumeration type, and if so, how should it look? >>=20 >> I like enums in general, but I'd like to note that there's already a = RFC in draft by Levi: >>=20 >> https://wiki.php.net/rfc/enum >>=20 >> As far as I know, the RFC is already fairly final and just lacks an = implementation. >>=20 >> So, I'd consider bikeshedding an actual RFC first. >=20 > If we=E2=80=99re bikeshedding, one feature I would really like to see, = with typehinting, is warnings if all cases of an enum aren=E2=80=99t = handled in a switch. So, for example, given our example Weekdays enum, = if I wrote this code: >=20 > switch(Weekday $someWeekday) { > case Weekday::MONDAY: break; > case Weekday::TUESDAY: break; > } >=20 > By providing the typehint, I=E2=80=99m indicating that I want to get a = warning/error that the switch does not cover all enum values. This would = be very handy if an enum value is added after initial development and = someone misses a switch statement in cleanup. >=20 > The typehint would also allow generating a warning if someone did = something like >=20 > switch(Weekday $someWeekday) { > //case =E2=80=A6 all the weekdays: break; > case =E2=80=98I am not a Weekday=E2=80=99: echo =E2=80=98Generate = a fatal error here because string is not a Weekday.=E2=80=99; > } >=20 > -John So, you mean like an implicit default: throw Error("Unhandled enum value Weekday::FRIDAY"); Possible, but then you also just can add default: assert(0); instead of a typehint. At compile-time won't be really possible, as, when the switch is = encountered, the enum might not yet have been loaded. That's one of the = consequences of PHP's lazy inclusion system... Bob