Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112196 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32912 invoked from network); 7 Nov 2020 16:16:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Nov 2020 16:16:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8B60C1804D8 for ; Sat, 7 Nov 2020 07:38:04 -0800 (PST) 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,HTML_MESSAGE, 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-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (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, 7 Nov 2020 07:38:04 -0800 (PST) Received: by mail-lj1-f178.google.com with SMTP id v18so4818839ljc.3 for ; Sat, 07 Nov 2020 07:38:03 -0800 (PST) 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 :cc; bh=BIzKFH+7WQTPK2r4qR1sYGJG0qF1uRZvmiFDgSW6mKM=; b=kz/tYpG1xj2KDvRlvZIQvCh1Iv/8BNpb77ycJsvCkgyIWCt5ed2KDW3l+ep3pQe44i vTDkzFYyynIP7SfihL64HWyrtN3ubkFILFOUBft5DSbztbVw8cdrdYM874aEGJN92tQS HTupcNpvGittAeJ518Hi9siXGZV/T7w1S2uEHk/koBD55xYOdqKdv6vOI3WVbHDhbAX9 8AQg3PT585OzypWVeoHgSV+P6kbYA011Yy2zypaGUR5kjWxJzttPHl6nPJ7ZPKcZequt kOcD2F+EUQGQdQVI5y4hfQLuKuXNtXWSOwkuJBs2rFHPyG/Eiv6p8ebQ5t6XkI5nrQBL /G4Q== 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:cc; bh=BIzKFH+7WQTPK2r4qR1sYGJG0qF1uRZvmiFDgSW6mKM=; b=Q1Ll81kXB+twHiZqOgaztLI6YA5BXF8OzBTYpoBL7o8fc/u2TYOk4pZFd2yKUpKv5Q IyffVKFtVRwN2Y0nMa/w7xBgAq7XbvdsNUC5gYlAvH5eAN7vexZ37LGqBzOk+0+2YsNR bDPbPNLr1CPlB7Oic/jINwGdVtkafj2GE3bBoTzLO6fLwl5ntRzAunVnEnMyUmDUmkDR hON9hsu4PxQUFQ1Azkad55czcq8vwyUZ+ojVs8EgLSrobTK2MgCYTbRppxXj4JTT7Gaw wOH1KU9Rrxj3ZP5YucwEQ9lmH/1POTGA50Tm64/ZxhbzmZHY+jZBNXtrcoERo2LsoIAg IOoQ== X-Gm-Message-State: AOAM530JxYXaz/tqnfl7v+rpvT2IfguJO87oOmd4NMZgWeAoLDlMHMQa qKlhxlvBRVelZdxvri/1nNTZVnvTwsFQ80jT/Gg= X-Google-Smtp-Source: ABdhPJwlbD995+DsvfIwANNpUa8SSCLqQa1Bob4u1LZDSvvM3WYwJH5MoU+rLNhudAeQMeb6wrTW+f/387x+a+hUblw= X-Received: by 2002:a2e:530c:: with SMTP id h12mr47460ljb.237.1604763480217; Sat, 07 Nov 2020 07:38:00 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 7 Nov 2020 17:37:49 +0200 Message-ID: To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= Cc: PHP Internals Content-Type: multipart/alternative; boundary="0000000000002fdd1a05b3861d33" Subject: Re: [PHP-DEV] Union `&` operator From: zsidelnik@gmail.com (Eugene Sidelnyk) --0000000000002fdd1a05b3861d33 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Yes. And I think it will be good if enforced by language. On Sat, Nov 7, 2020, 5:33 PM Olle H=C3=A4rstedt wr= ote: > 2020-11-07 15:12 GMT, Eugene Sidelnyk : > > When you follow ISP, you probably would have a lot of interfaces. > > Thus, client code may require an object to implement a bunch of > interfaces > > depending on functionality needed. > > > > Consider class `Bar`: > > > > ```php > > > > interface A {} > > > > interface B {} > > > > interface C {} > > > > interface D {} > > > > interface E {} > > > > class Bar implements A, B, C, D, E { > > // > > } > > ``` > > > > > > And some code, which uses `Bar`, but depends rather on interfaces, whic= h > > functionality is actually needed: > > > > ```php > > function foo(A & B & E $object) { > > // some work > > > > var_dump($object); > > } > > ``` > > > > It is currently feasable like this (ugly): > > > > ```php > > function foo(A $object) { > > (function(B $object) { > > > > (function(E $object) { > > > > // some work > > > > var_dump($object); > > > > })($object); > > > > })($object); > > } > > ``` > > > > Or like this (more readable, but still): > > > > ```php > > function foo(A | B | E $object) { > > > > if (!$object instanceof A) { > > throw new \RuntimeException(); > > } > > > > if (!$object instanceof B) { > > throw new \RuntimeException(); > > } > > > > if (!$object instanceof E) { > > throw new \RuntimeException(); > > } > > > > // some work > > > > var_dump($object); > > } > > ``` > > > > Another possible option is to create interface, which combines `A`, `B`= , > > `E`, but in some cases it would lead us to interfaces explosion. > > > > This idea proposes to eliminate `instanceof` checks by introducing `&` > > union operator. > > > > You mean intersections? Psalm supports this notation. > --0000000000002fdd1a05b3861d33--