Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88324 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52406 invoked from network); 18 Sep 2015 14:11:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 14:11:25 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.51 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.213.51 mail-vk0-f51.google.com Received: from [209.85.213.51] ([209.85.213.51:34308] helo=mail-vk0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BC/6A-19961-C8B1CF55 for ; Fri, 18 Sep 2015 10:11:24 -0400 Received: by vkhf67 with SMTP id f67so30639123vkh.1 for ; Fri, 18 Sep 2015 07:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type:content-transfer-encoding; bh=Wc/PU1KoJG19W1RCwVQDVvtsapW6V6wgaCoticgcGBM=; b=X27qAHNIcAYIPC8xz1/cFTpQzCSJWpkTZHmdFNqu1NrUo15XdcT2Qdiyh0Wd1Q6dP8 frkFHrGThf8H7QhUn3NQoQFp2MeTtQOaoo86kmhVfKsXjhzowz0bbC5JnTiz4lsx60ar VzTHTTWPk/9Phu1jB5LZKDoKJO4qPkLzrPaxPtfg/H98cOXSa/EBF2D4GAQGOYQtYsCa 65DCcZh/kkxojvv9lTkYdM1ltSu7WM6M0IYpqXfBWEJLQp/jBrwRKdutXT6RvW22uSFv jEbOAmyMeO5/KOm0a65wPvrtQY1xtg3nKKB0IW+u+Dd/VsCw11Js18ni9lsYppAiGZIv +mAw== MIME-Version: 1.0 X-Received: by 10.31.8.68 with SMTP id 65mr3300682vki.154.1442585481635; Fri, 18 Sep 2015 07:11:21 -0700 (PDT) Sender: morrison.levi@gmail.com Received: by 10.31.41.205 with HTTP; Fri, 18 Sep 2015 07:11:21 -0700 (PDT) In-Reply-To: References: <55FB4791.4040807@gmail.com> Date: Fri, 18 Sep 2015 08:11:21 -0600 X-Google-Sender-Auth: AlJ9922xI7DkX3imCyU4eGEG9SY Message-ID: To: John Bafford Cc: Bob Weinand , Rowan Collins , internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Let's discuss enums! From: levim@php.net (Levi Morrison) On Thu, Sep 17, 2015 at 5:52 PM, John Bafford wrote: > On Sep 17, 2015, at 19:16, Bob Weinand wrote: >> >>> Am 18.09.2015 um 01:06 schrieb Rowan Collins : >>> >>> This has come up in passing a few times recently, but I'm not sure ther= e's ever been a dedicated discussion of it: would it be useful for PHP to h= ave a built-in Enumeration type, and if so, how should it look? >> >> I like enums in general, but I'd like to note that there's already a RFC= in draft by Levi: >> >> https://wiki.php.net/rfc/enum >> >> As far as I know, the RFC is already fairly final and just lacks an impl= ementation. >> >> So, I'd consider bikeshedding an actual RFC first. > > > If we=E2=80=99re bikeshedding, one feature I would really like to see, wi= th 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 t= his code: > > switch(Weekday $someWeekday) { > case Weekday::MONDAY: break; > case Weekday::TUESDAY: break; > } > > By providing the typehint, I=E2=80=99m indicating that I want to get a wa= rning/error that the switch does not cover all enum values. This would be v= ery handy if an enum value is added after initial development and someone m= isses a switch statement in cleanup. > > The typehint would also allow generating a warning if someone did somethi= ng like > > 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; > } I've looked into this but not in the same syntax you've provided. I was exploring a `match` construct that has different semantics: match ($foo) { case Weekday::Monday: { echo "Monday"; } // it is not possible to fall-through with match/case case Weekday::Tuesday: echo Monday; // single expression doesn't need braces } // if it hits the end without a match a runtime error will be emitted I think this fits into the dynamic behavior of PHP a little better than forcing a case for each enum value at compile-time, even if it was possible (I suspect it is not).