Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88307 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95318 invoked from network); 18 Sep 2015 00:48:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 00:48:59 -0000 Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.92 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.92 blu004-omc2s17.hotmail.com Received: from [65.55.111.92] ([65.55.111.92:63922] helo=BLU004-OMC2S17.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D1/22-19961-A7F5BF55 for ; Thu, 17 Sep 2015 20:48:59 -0400 Received: from BLU436-SMTP222 ([65.55.111.71]) by BLU004-OMC2S17.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Thu, 17 Sep 2015 17:48:56 -0700 X-TMN: [awEgtVREVO13TclhmF1m4OF4/WH7YdMg] 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: <49A20454-0E55-4667-BBB4-267742201DA6@zort.net> Date: Fri, 18 Sep 2015 02:48:52 +0200 CC: Rowan Collins , internals@lists.php.net Content-Transfer-Encoding: quoted-printable References: <55FB4791.4040807@gmail.com> <49A20454-0E55-4667-BBB4-267742201DA6@zort.net> To: John Bafford X-Mailer: Apple Mail (2.2104) X-OriginalArrivalTime: 18 Sep 2015 00:48:54.0208 (UTC) FILETIME=[CB0D1C00:01D0F1AB] Subject: Re: [PHP-DEV] Let's discuss enums! From: bobwei9@hotmail.com (Bob Weinand) > Am 18.09.2015 um 02:35 schrieb John Bafford : >=20 >> On Sep 17, 2015, at 20:06, Bob Weinand wrote: >>=20 >>> Am 18.09.2015 um 01:52 schrieb John Bafford : >>>=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 >> So, you mean like an implicit >> default: throw Error("Unhandled enum value Weekday::FRIDAY"); >>=20 >> Possible, but then you also just can add >> default: assert(0); >> instead of a typehint. >>=20 >> 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... >=20 > Compile-time might not be possible, true, but at least this would = provide for runtime checks, and the benefit of explicitly stating = intention is that intention is explicitly stated. Also, it would allow = the sort of errors I=E2=80=99m proposing be generated to all be = standardized across all applications, which would allow fancy error = handlers, such as with Symfony, to offer helpful context-specific = suggestions. Also, it=E2=80=99d provide static analyzers additional = information by which to provide correctness alerts. >=20 > -John Static analyzers etc. all can look at the cases and conclude that the = type indeed should have been of a certain Enum. I don't really see a = particular advantage here. And for the run-time checks, just add a default: assert(0); as said. I = don't see loads of benefits from this extra syntax. Also, as said, I'd pretty much prefer that to be separated out in a = different RFC as it isn't crucial to the Enum feature itself. Bob=