Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113885 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41744 invoked from network); 31 Mar 2021 18:54:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Mar 2021 18:54:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4E998180087 for ; Wed, 31 Mar 2021 11:52:26 -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-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (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 11:52:25 -0700 (PDT) Received: by mail-yb1-f181.google.com with SMTP id j2so22246453ybj.8 for ; Wed, 31 Mar 2021 11:52:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=0cXInqssMOHQiI50ie8yH3D3Aq4Pkc2h3W3IgXPlVVs=; b=dABxIHhsYSxWBLmiY01GRGuvhG1W4/V+cHaZHREeHlyMhYJ0RwUS2fDIAmuOG7GO5/ nUl5J0lhHBixSKVk/9XcWzlsIyI1/uCVDQUlj1pgF2bAKH6t2hlzPYE/0TR6AlWmwQxs t8cuE9wIz97nlyBYNJXfiovGDkyKfbGc7anelMaN5fHZv7dSLUUshsaO9DB22Pt/h33U HAZ+EkyUOmTaimSqXij9rQvG1RqpZOR3j8lQLcHTKTj4pqw67r3Alzn+ThWGnZ6VmJ/o rYq2NZFnOVQvfOBAr/PGAtwFVjJp034MH3VEZPY7zIv2H5stlVztKCI0eHJ+YazIbCNb 23aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=0cXInqssMOHQiI50ie8yH3D3Aq4Pkc2h3W3IgXPlVVs=; b=ArRy+uUkxrLg9zaPT6+jEQCEQttr8HOMmao6DrEhagYDFCX7Phm5SE+EmERpHKztik p4g4Sbz8fJi9cp/RjuyG6YtnfRky2qqwgV8aFugzwmedDMsT4FEESR5emvboSaVXQdBa FlRuRKGBUysVI0w5hM8O1yf/zHhiOhjgnsfqgVh2WnUT7JCRHdg4JB4AlhAzfh+KFkEQ i7+dSny95EGNMekKFW17DN57Y5dNSmeXedmE2Rreyd3+WXp27zB7abi0leRjc8b5N6U8 OZkGQeSa+RmTQUmFNEHA7OuKLzX9m4VpY0S/eaCM1L4Vu91mt8jPZDWNO2+79oeAXUbe TV6w== X-Gm-Message-State: AOAM531WAuq49/pSvHWP+EkYE8rvO85LZSye4gd8S+LYc7WcxGRomtQl yR/XoY2c3i9ptHgMUrcIGRZcNOFI+vOuazVTaAZTMoGv/rBkRA== X-Google-Smtp-Source: ABdhPJwY0J6r5+gPuq8RmfQ4RBExG9rq9W37vF4dLrA85UbvB5lDA5FZrUU5b+/J7RwPMG0Zwi7Lx3//0bEQjf+Kmgg= X-Received: by 2002:a25:7801:: with SMTP id t1mr6646035ybc.48.1617216744832; Wed, 31 Mar 2021 11:52:24 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 31 Mar 2021 20:52:13 +0200 Message-ID: To: 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: tovilo.ilija@gmail.com (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