Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114145 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82803 invoked from network); 25 Apr 2021 07:35:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Apr 2021 07:35:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9A9451804CC for ; Sun, 25 Apr 2021 00:39:41 -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 ; Sun, 25 Apr 2021 00:39:41 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id x19so53230031lfa.2 for ; Sun, 25 Apr 2021 00:39:41 -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=ahmfFxLgX7SekqmQgXAbdo1zmNPs25FKe6iO360u+6A=; b=P1mFViPIIIX+FafggwSgoAOLOq5dQIxx09pdGOYw1T83j06nu3xJqlWNV7iKZ05Dvq FHmf4xY2nl+lWubx4zMlyNwbo3QhWLNBvWX/dyFiu/ZkBgzD/qLsPzR2MDRBhFKdSnIX omaOQr2jLvoNPzR/GSk82toEzcWziLOW2ztKK5yQsXzw8nLCWGU9t9POCjez4hOZDM56 +FqoVXQOWGqllmdXcHVSEAzNnkU1s8ZD7nyi+6o2jFxIp6drlJaoyA9/GfgE3dhHQPhC 3elldr1v4xJPuxTvFx1ts/ZfUa1Ql6A/uYhtAG8yGcdxJpUblPV/5k6pXkgCtXR60FWt jn+Q== 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=ahmfFxLgX7SekqmQgXAbdo1zmNPs25FKe6iO360u+6A=; b=DWjG4bYDeuAXn5dao1njGKUf+iGcqokPK+GVCDjVPmflvj5Hd0SQSayF/obufGwgE9 +5GvL91rCeQdmQeGk5YlWt3XxRVXBlFD9M5ObJZxRcdUg2uYV8gl1ANIOBqAX5UmilDS 7LuYlqr0vuJrMyAkDag39ibDdYDRpF+sI6q45G1oCkKgirMKWokV9TCZO8/DVo6DLeUe JM0b52AA2XMHonN/4W+WUhc3Fbn1pdGXvr+A8qCtXIiQTA3R/WC529w4VIuqVF7En4Pz afDdhmGTTtVMghO3Got8tt0cPVnOmhTi347yRqfEwiTfM6roVClQr8U6Fo5NBeesV8LH 6rpg== X-Gm-Message-State: AOAM5316adNyfxAq6S+Lc373HVSQq4iKQP3Fa4ordNFpCaCL5gJXElvG TP9llyHJZtSyqLy6Lj4bDyLdHXDIscsy9DM12ec= X-Google-Smtp-Source: ABdhPJwvOqU6ujAHw3v/po1VToe943Cn2GQxR641vr+HSte1fQ2BQaBiyJnpjTB1pyszK3kq3wPs+krQmPvjIOW+/EQ= X-Received: by 2002:a05:6512:3147:: with SMTP id s7mr8376435lfi.41.1619336378818; Sun, 25 Apr 2021 00:39:38 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab3:4e13:0:0:0:0:0 with HTTP; Sun, 25 Apr 2021 00:39:37 -0700 (PDT) In-Reply-To: References: <5b9f1500-615a-48f1-815f-1d48b327ef90@processus.org> <179049b1475.11134368b213512.254739612773841999@void.tn> Date: Sun, 25 Apr 2021 09:39:37 +0200 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Draft] Sealed Classes From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) > In practice, I think all of the use cases for sealed classes are ADT-esque. > As I noted before, combining sealed classes with Nikita's new-in-expressions > RFC would allow for this (also using my short-functions RFC for this > example, although that's a nice-to-have): > > sealed class Maybe permits Some, None { > > public const None = new None(); > > static public function Some($x) => new Some($x); > > public function value() => throw new NotFoundException(); > > public function bind(callable $c) => static::None; > } > > final class None extends Maybe {} > > final class Some extends Maybe { > private $val; > private function __construct($x) { $this->val = $x; } > > public function value() => $this->val; > > public function bind(callable $c) => new static($c($this->val)); > } Yes, the Maybe/Option type is a good example! Because you know there will never be another extension. But it's worth noting that whenever you do *not* know that, these concepts suffer, even in functional programming, by the same issues as I mentioned before with regard to maintainability - you can't easily extend it without touching old code (there are attempts to fix this by making algebraic datatypes extensible, but it didn't get widely adopted AFAIK). Also see this thread about the expression problem: https://stackoverflow.com/a/871375/2138090 Disregarding the limitations of maintainability, the real power of algebraic datatypes is of course the pattern matching functionality seen in OCaml and Haskell. I'm leaning towards tagged unions + pattern matching have more to offer PHP than sealed classes (and even more so when pattern matching can be extended with guard clauses and catching exceptions). The RFC author(s) might want to extend the RFC to reflect the relation to tagged unions, and how they overlap (or not)? Olle