Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88360 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24328 invoked from network); 18 Sep 2015 21:31:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 21:31:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; 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: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.182 mail-wi0-f182.google.com Received: from [209.85.212.182] ([209.85.212.182:34747] helo=mail-wi0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D4/B2-09127-3B28CF55 for ; Fri, 18 Sep 2015 17:31:32 -0400 Received: by wicfx3 with SMTP id fx3so79653960wic.1 for ; Fri, 18 Sep 2015 14:31:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type:subject:from:date:to :message-id; bh=82V+bj4/Rxzry1oVzihCUJjw6Hu2I0tUnllek7/ryBk=; b=r1uduou5+o34aPniAJo8GI89L9z6/tf8jDKXtjDuM8y5ZN5v+HEGFhKU5nMMJg6+5s CcSdPHLh8LVJIyeTW0QuJUOSQ4j4jpy7G4NVP/3kM+9APs8SiVGLoQJX/nUH30Z+2m6E Wx/k7qq8oRbYbyZ6zsXUvm7bBXaCRnlw5EtzAt50UJNhbnlfNIsfAipsF9iELFt/9MzV RFfRQU1WZuI2dzLTKgAL5i7F9Z4VnH2NLh3zJPXnuLmas70ZXIRTuBAjcKPaoEz9e0td nKKUKddm7wkda/0KxTQLOwS3N8FASOb5c+TiUU2VnMrY51KHNKYJeNdu7sg89Zb+U+9l tK4Q== X-Received: by 10.180.75.137 with SMTP id c9mr380005wiw.16.1442611888178; Fri, 18 Sep 2015 14:31:28 -0700 (PDT) Received: from [192.168.0.6] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by smtp.gmail.com with ESMTPSA id t7sm116696wib.1.2015.09.18.14.31.27 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 18 Sep 2015 14:31:27 -0700 (PDT) User-Agent: K-9 Mail for Android In-Reply-To: References: <55FB4791.4040807@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Fri, 18 Sep 2015 22:30:17 +0100 To: Anthony Ferrara ,"internals@lists.php.net" Message-ID: Subject: Re: [PHP-DEV] Let's discuss enums! From: rowan.collins@gmail.com (Rowan Collins) On 18 September 2015 15:42:44 BST, Anthony Ferrara wrote: >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 = 0; >const TUESDAY = 1; >const WEDNESDAY = 2; >const THURSDAY = 3; >const FRIDAY = 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. >} I think this is a fundamentally different style of enum to what Levi and I are thinking of. In your example - and this seems to be what C# means by enum, for instance - it's a subset of a scalar type, usually an integer, with some names as mnemonics. Other than that it behaves exactly as the base type would, and can be readily cast, maybe even implicitly, to and from that base type. The other style is where each enum type is a completely distinct type, not just a whitelist of values in some other domain. In a sense (and this is what got me thinking about enums recently), you can consider null to be an enum with only one value, and boolean to be an enum with two - you can define (int)true as 1, but true is not an integer, and 1 is not a boolean value. I suspect both styles may have their place, but algebraic types certainly wouldn't take away my thirst for object-like enums. Regards, -- Rowan Collins [IMSoP]