Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88305 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92172 invoked from network); 18 Sep 2015 00:36:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 00:36:07 -0000 Authentication-Results: pb1.pair.com header.from=jbafford@zort.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jbafford@zort.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zort.net designates 96.241.205.2 as permitted sender) X-PHP-List-Original-Sender: jbafford@zort.net X-Host-Fingerprint: 96.241.205.2 nova.zort.net Received: from [96.241.205.2] ([96.241.205.2:40501] helo=nova.zort.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/71-19961-67C5BF55 for ; Thu, 17 Sep 2015 20:36:07 -0400 Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (authenticated bits=0) by nova.zort.net (8.14.5/8.14.5) with ESMTP id t8I0ZxbF026843 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 17 Sep 2015 20:35:59 -0400 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) In-Reply-To: Date: Thu, 17 Sep 2015 20:35:58 -0400 Cc: Rowan Collins , internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <49A20454-0E55-4667-BBB4-267742201DA6@zort.net> References: <55FB4791.4040807@gmail.com> To: Bob Weinand X-Mailer: Apple Mail (2.2104) Subject: Re: [PHP-DEV] Let's discuss enums! From: jbafford@zort.net (John Bafford) > 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... 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. -John=