Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88328 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59258 invoked from network); 18 Sep 2015 14:42:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 14:42:48 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.182 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.212.182 mail-wi0-f182.google.com Received: from [209.85.212.182] ([209.85.212.182:34417] helo=mail-wi0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 82/DB-19961-6E22CF55 for ; Fri, 18 Sep 2015 10:42:47 -0400 Received: by wicfx3 with SMTP id fx3so67322041wic.1 for ; Fri, 18 Sep 2015 07:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=tbl75ewYM1cWEhLU6+Z2Ann82yiXuz469BX9SKu5R6I=; b=SwZWCA07xwKXCpntUUcVplAre+jwd5dvMCpubAMHhII7JO6wBQ3t4zlyTcmn+x2ExW WKgLVfZAqNg+K2BfDa3DqgDTNO+Ux+hYJqC44qM5k9KuxTjW3FdEzF4q7/0mQRsCRVHA 48R8obFlVUYMKWVp9Yz1r7Nwf5gxxWpjMJatzwZM9dOXEZUyrn2aWW9fxmyfcmlqHPNj kPsJfvgOUsWKQeC9NZinhyMovYaCyqrGoHAkbtQrFRemCKh7PoscmFzhlWbTXOsIbp40 7vfzJ99dTcOgRqsxzYxmCvYKZc8tnVtdadnyzSFyPeTFoOsweLJI9YdNH2jcKmXBaDPe rr7A== MIME-Version: 1.0 X-Received: by 10.180.211.10 with SMTP id my10mr17267105wic.84.1442587364181; Fri, 18 Sep 2015 07:42:44 -0700 (PDT) Received: by 10.28.55.18 with HTTP; Fri, 18 Sep 2015 07:42:44 -0700 (PDT) In-Reply-To: References: <55FB4791.4040807@gmail.com> Date: Fri, 18 Sep 2015 10:42:44 -0400 Message-ID: To: Levi Morrison Cc: John Bafford , 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: ircmaxell@gmail.com (Anthony Ferrara) Levi et al, On Fri, Sep 18, 2015 at 10:11 AM, Levi Morrison wrote: > 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 the= re'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? >>> >>> I like enums in general, but I'd like to note that there's already a RF= C 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 imp= lementation. >>> >>> So, I'd consider bikeshedding an actual RFC first. >> >> >> If we=E2=80=99re bikeshedding, one feature I would really like to see, w= ith 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: >> >> 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 w= arning/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. >> >> The typehint would also allow generating a warning if someone did someth= ing 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=98Generat= e 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 emitt= ed > > 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). This is basically pattern matching. And while I'd LOVE to see support for it in PHP, I think it's a bit off-topic here. Perhaps a new thread could be opened for it (or ideally a RFC)...? As far as enums, I wonder if they would be necessary if we supported algebraic types, since you could define an "enum" as a virtual type: const MONDAY =3D 0; const TUESDAY =3D 1; const WEDNESDAY =3D 2; const THURSDAY =3D 3; const FRIDAY =3D 4; use MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY as WEEKDAY; function foo(WEEKDAY $day) { // must be an integer 0-4, or castable to 0-4 unless strict_types is on= . } Just a thought... Anthony