Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113886 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45333 invoked from network); 31 Mar 2021 19:19:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Mar 2021 19:19:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 09C6A1804D0 for ; Wed, 31 Mar 2021 12:17:12 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 31 Mar 2021 12:17:11 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id b4so30696267lfi.6 for ; Wed, 31 Mar 2021 12:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=YjLMK4tlyoZ0F7QCzE/gz/cs67989BMJwSFTV9tA9YI=; b=EQMn6VcX8KZMX4bU7/x22r0wZCIaqUJCKmkt2y3z6dxnocQ009gx3rmDNwGvlX+Q7i hRX50yxeXeGSXoh6GUCi5OEHXT4Y/LKB1q8s/rqpOpJMPxgpnWECUXQJMRSF2qoAm0xS wUe1xxhJ64U61H4zL9IT8v7dqtlv25DEVQI7q1nPh5m4/TFXajHYE83wctYA1bSsIe5z HRkG0RIVBc4R25TzQbTFK5aek1uysUpzZSd0Fr1RwtXqHz11WhtRuf9BADONrmAqVdIA 8VeMNb5hNODbqelWmD24K+8MGUazrxjB660LQoWzXiTWc7hf50l2y0LPtd7D7TCyJ2le 4fJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=YjLMK4tlyoZ0F7QCzE/gz/cs67989BMJwSFTV9tA9YI=; b=MSwYdMoxPcxed9ebtpUE2T8xZqqUe/Pl6p75VlvSQK3gEkp9+BOnJl/Aw6WqahQu1N CNKWZFzBSr3keVIipdAhJldBMcL9RYa810yqCU0cjCRlXPNHgd1GB9KEVHf9KDHyVHIN fflxC8LaGJ/h5Rm/p64RYTGVggD8fBUOD3bV4JGdmm4fy2X/Opkru+2Rvk1vofvtrcUt ABngjWD8m43qmlHnxQgI2wJ1/bWPBgRf/q6eBTbQ4BEuxsFr9Yp9mwDLsydwN7Agdva7 CzfL/17np6/B+LK9QQi9/Tr8yvfhSQwvmuJsAOpzvtXjYhynmdXvYkaMguZzoPgq7Liw 35TQ== X-Gm-Message-State: AOAM532Qe4D50CJFjXySU7ZlAJ3qcSLpYxDm+W/hew/UQ0VLZOO/pO8o 2tCm7RLbcR+q+gsv5IEulzomCiPBas+d91MFOl0= X-Google-Smtp-Source: ABdhPJy2mPskQvem8XBMimL9md1XUliDQqbHf8uS42PjxUYP2G77OU2RM3Cf7v8fJ9oh0RSqNb8Grkqjy5hU1a0sU2E= X-Received: by 2002:a05:6512:3d1c:: with SMTP id d28mr2946862lfv.41.1617218229450; Wed, 31 Mar 2021 12:17:09 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab3:5f8f:0:0:0:0:0 with HTTP; Wed, 31 Mar 2021 12:17:08 -0700 (PDT) In-Reply-To: References: Date: Wed, 31 Mar 2021 21:17:08 +0200 Message-ID: To: Ilija Tovilo Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Any reason why backed enums are limited to int and string only? From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2021-03-31 20:52 GMT+02:00, Ilija Tovilo : > Hi Olle > >> I was trying to implement the result type from OCaml using enums, and >> noticed that they can only carry int or string data, not arbitrary >> data. Any specific reason for this? >> >> Example: >> >> enum Result: mixed { >> case Ok = null; >> case Error = null; >> } >> >> Error with: >> >> Fatal error: Enum backing type must be int or string, mixed given in >> /tmp/enum.php on line 3 >> >> More specifically, for this idiom to work, "Ok" needs to be mixed and >> "Error" should be string. >> >> OCaml idiom: >> https://ocaml.org/learn/tutorials/error_handling.html#Result-type > > What you're trying to achieve here is not what backed enums are for. > Backed enums are for assigning each enum case a single, constant > representation that can be converted back and forth. As for Result, > what you'd want here is a dynamic, arbitrary value for both Ok and > Error, which is exactly what ADTs / tagged unions would allow you to > do: > > https://wiki.php.net/rfc/tagged_unions > > enum Result > { > case Ok(mixed $result); > case Error(mixed $error); > } > > Optimally we'd define Result as "Result" but that's > another discussion :) > > Note that work on ADT hasn't started yet. > > Ilija > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > Got it, thanks guys! Olle