Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114134 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21223 invoked from network); 24 Apr 2021 17:35:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Apr 2021 17:35:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BDEA11804DC for ; Sat, 24 Apr 2021 10:38:52 -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 ; Sat, 24 Apr 2021 10:38:52 -0700 (PDT) Received: by mail-yb1-f181.google.com with SMTP id v72so38794252ybe.11 for ; Sat, 24 Apr 2021 10:38:52 -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=m6vD/KXEuhDCSOgru+IElGVbefVsp6JLAOquCUj5CTA=; b=Qnm82s/oxl8CIWQEYKFQBDM3ua+or9PHHbqtPMaA6CFM5dPtkHKkEDmJoD/wkKu/LX 9fVITohMKSolrlaz9JTC9yNGRGybucOitGMDWBRHFJQwVIn3MwcmcfwNqDGjRZxNgkAA c6/1tkHPIIO5BvDJ1bg5Mu9huJYKJBd9qMYTJr6IepPMkF9A+KfrbtDVT/yDPB7vC2hJ NEjPv6BSsaT2a/vxPc7eHbeFMzI6B5QcS1Jjr7Kifo69g/tgJaqXpAVk+BGw24acBhQL bbnPh7p2C+SFG3ikNcQZTKmSF8wvnxvJpI8pzHUWrzDahOXJQfbAsz7oJ6kHv4srx5a8 CRtg== 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=m6vD/KXEuhDCSOgru+IElGVbefVsp6JLAOquCUj5CTA=; b=HxIYcH0GmFwIGoHKoRcFLe5DD1QvlVLHQg9PuVgMLRg/9qhOM2s2cUh9Wa/6Np5GRR 1MF5G/eomRaTDhzC5s+3qdiJxkIyOAmD96hRba5gHNg/lnGNH6OlTdj92mDdpfXG1pw1 wP1lW1OaO+krqFFQUcXIXzZhoEkFfnMkPIC0W7DPb46M6MKm/jIk6EPCrNBanyKfhmX5 sx7LgTa5TGaWAS/m89gL0tKvD1pItH9lelYMKctuEplYI1HHUwuALoMtzLtPs3IfKHEU FseNqNsNcQPvUDzWitAVcF6t7yVmvSZ6lpdrMbo8faU4gpPTS7ZTg5HyerOvwbesTCrb NGqg== X-Gm-Message-State: AOAM531SdxrilrBM4/o7v79KV7KFSq4zIJrUJyHvae+mlYweZYwvE38M KBNzDalDFluw3Av7nMQg33U5AZ71WN/WSIX9C+0CQb48TpJ8FQ== X-Google-Smtp-Source: ABdhPJwRaLDVYVn+vzGaXSws0QQknhDZhrU3u2TgeB2c9XpPct9cgiQ6yDqsa2fs/1nKKavGjvKbpTQJ0Wtmlztqrmc= X-Received: by 2002:a25:a241:: with SMTP id b59mr12464349ybi.411.1619285931367; Sat, 24 Apr 2021 10:38:51 -0700 (PDT) MIME-Version: 1.0 References: <17903a93d74.120722efe200673.5873031933651438902@void.tn> In-Reply-To: <17903a93d74.120722efe200673.5873031933651438902@void.tn> Date: Sat, 24 Apr 2021 19:38:39 +0200 Message-ID: To: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Draft] Sealed Classes From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Saif On Sat, Apr 24, 2021 at 1:35 PM Saif Eddin Gmati wrote: > > Hello Internals, > > I'm sending this email to open discussion about sealed classes, interfaces, and traits feature for PHP 8.1. > > I have create a Draft RFC here: https://wiki.php.net/rfc/sealed_classes > > A major concern for few people have been the syntax, in which it > introduces 2 new keywords into the languages, therefor, i have added a > section about alternative syntax which could be used to avoid this > problem. As mentioned off-list, I think there's some overlap with ADTs. https://wiki.php.net/rfc/tagged_unions There are two major motivations of sealed I can think of: 1. Usage in libraries for classes that can't be final because they're extended by other internal classes. This prevents them from being extended in user land and used in unintended ways, which reduces risks of breaking changes. 2. For types have a fixed number of subtypes. This makes it possible to handle a value by type and knowing that you've handled all possible cases (exhaustiveness checks). Good examples are Option (https://doc.rust-lang.org/std/option/enum.Option.html) or Result (https://doc.rust-lang.org/std/result/enum.Result.html). It could even be argued that 1 and 2 are the same. Enums/ADTs essentially try to solve the same problem. This example is taken from the RFC and rewritten as an enum: ``` enum Shape { case Circle; case Square; case Rectangle; } ``` Obviously this is the simplest case, but enums/ADTs allow adding arbitrary data to each case and arbitrary behavior to the enum itself. This approach differs from sealed in a few ways: * Sealed allows (or requires in terms of autoloading) separating subtypes into multiple files * The subclasses of sealed classes are allowed to have diverging APIs, while methods in enums are currently only allowed on the enum itself. This could be changed but feedback from the enum RFC has shown this is not desired by most people. * Sealed could be used for existing classes without breaking the API too much. * The syntax for enums is much more concise * For cases with no data enums will create a singleton, while sealed will require manually managing a singleton or creating an instance for each value (e.g. new Option\None). There's probably more, these are just off the top of my head. This begs a few questions: * Are these approaches different enough to justify both of them? * If not, which is the preferable approach? Ilija