Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112197 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63290 invoked from network); 7 Nov 2020 20:24:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Nov 2020 20:24:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3EC661804C3 for ; Sat, 7 Nov 2020 11:46:09 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (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 11:46:08 -0800 (PST) Received: by mail-ej1-f53.google.com with SMTP id oq3so6676427ejb.7 for ; Sat, 07 Nov 2020 11:46:08 -0800 (PST) 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:content-transfer-encoding; bh=VfTYw4V7Dn/vzhHD78Jc2S5YeC2iLYAjUVvFC1yEYbI=; b=ARPYy9pKqkPY4m7VUpIGyGNCGG/Wmai/3eSD4gqG9s6qi50oa4JDBjqa9NwtiTNuTO DZL8EbvSjARF2SIjO/fZyC0FWYjwzIa+0hVFQmCWO7iThkXMflJwk2C8zi+WR72gzrWq k3bKF3yn6WH49O5VEYDO7CUCZmaIFI5SiSITNWqG+8iHHTXby7cI/+dK4uN11uw0IfVp NIgoOg0/4lyGdZoBT0TQg6HHeJH38BKe2M+9M0HWPGl++/qYmrgJkdi+LRdDd3qmAeyR KK4xkMD1lzICUR6SiUYn3/vSV4AaLCwFbWj1WrpMMbphSL6865TlDxzlUCuoirt9nmRS xRyQ== 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:content-transfer-encoding; bh=VfTYw4V7Dn/vzhHD78Jc2S5YeC2iLYAjUVvFC1yEYbI=; b=akyvfGgubuGpDPpwlBirAH8lMvZzL6oRkGPlfzbFJTQYQMYbvU7yiQcvMXZMOo/VqQ KRDBi6k/WiHa5nQzBuS7dJvci1/2jsJ+IkTvDXG5WWlzPqsiM/NBl750lqoChkoLd5TU q/4s5FZ7NPyr8xj3mKX4/+ogL3O4eYmR96YIDEfL8Z83VTf2hxMlsRO9GZdYpBr+Mr8a VCRm1kx6PHT7nt5psU6JdrTcD5CSgjC4xHsv+4SM8fAR8dHTfsg0VEzYtqq0vqsSyKyT JRN/f0gLabiQOFXZPCU2yzUtXbjKKN5A1BgmcJH6/d0lwDhSmbv2EXdNTKyDPx504o1m LxyQ== X-Gm-Message-State: AOAM5327Ch5P0lsQWT+OS4iwmPy2pUMo2KUVrTpFRtvkhgQr1EIP5TxA 9kTubF7+k/LB6p2QbvfLA3/5zlWD98PaqzLMQ/I= X-Google-Smtp-Source: ABdhPJxxOQ8wrsTY41n1rCmBHjtGuWYcH2lCk9/daEh4zo8VlyoxYJ2iE+vgPhwWXiVxx8ddtpE9bJHNehPrDADldpU= X-Received: by 2002:a17:906:16d6:: with SMTP id t22mr8256194ejd.376.1604778367332; Sat, 07 Nov 2020 11:46:07 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab4:a4a4:0:0:0:0:0 with HTTP; Sat, 7 Nov 2020 11:46:06 -0800 (PST) In-Reply-To: References: Date: Sat, 7 Nov 2020 20:46:06 +0100 Message-ID: To: Eugene Sidelnyk Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Union `&` operator From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) https://wiki.php.net/rfc/intersection_types 2020-11-07 16:37 GMT+01:00, Eugene Sidelnyk : > Yes. > And I think it will be good if enforced by language. > > On Sat, Nov 7, 2020, 5:33 PM Olle H=C3=A4rstedt = wrote: > >> 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, >> > which >> > 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. >> >