Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112195 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31111 invoked from network); 7 Nov 2020 16:12:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Nov 2020 16:12:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B20D11804C3 for ; Sat, 7 Nov 2020 07:33:27 -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-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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:33:24 -0800 (PST) Received: by mail-ej1-f51.google.com with SMTP id za3so6139955ejb.5 for ; Sat, 07 Nov 2020 07:33:24 -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; bh=bOBo4JQ302U24GBfwhMxUA+9FdycYw/xI94j1f2wp/A=; b=AUry2z5pWvvGWrNO1OZaiVLBylY4AikLauvLYSywOmwhSqCBFoJ6ANf3VETN1IQJyK iwwKOulzyZmlPuhucxbnJFtgBhxfXohfdROmJvHwk0ilji3KMUQh0Ek9V0U9yzzFyEzP OyM5CfiHsDDgR7Yl7LrJV1ki8tzx7zpFf4nmKxFsmQ4x+jbkKKhlILdYq7xq2aB75UwV x0JSQyTtoKKnM3gtiE+0tcNy3hr7/qlo2q0Nw9E7vc+4stNR3fxrtSg6m9OWiWfRbbXu n1O8oEjHhGMs9KldA/opZcH+kUaACE+WWfvovjVV6eTee2pvAr+0APz08ZtGuo6J1PCp i45w== 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=bOBo4JQ302U24GBfwhMxUA+9FdycYw/xI94j1f2wp/A=; b=AfQ2MFGu0iNT+Apeh/vZSV8TZYjp0rod/fpbdbwQQuAKmupMhO3tme059xM6oHUWin QQabmjj4/uFCHjp+SZe2tP1dVnGiL1irouSnLNaXsXEfX0zfZkpoKUxfOrEwZ8YH2XGo k3RbtfcMuSg99tqH2tVYgFMq1pCel1Ahiy20ZibOg+A8gayiJd4adg2thMfNebCvDCdU HWAavZMvaxUrqLR9D7/WCSkCe9nfhuMuG2mF43akwMDBq+AZGvKHmQR+bqnYPeQN+F92 ybFG+r7ARaVNmuouGJfqrn/ObCinGdATwBmmQjcZPqzE0l09lc1Bp0yL0dMgifdwXdql ezNA== X-Gm-Message-State: AOAM531h3V71kVISxqQ54d/oXl+CSy4U24Vef9ula55pX9rm0Rjv4O2/ 3ge1lHXQTtW5PUqhOE+sokkcHoduH7woVcZM9xw= X-Google-Smtp-Source: ABdhPJy+7xdBsF0ReEc4DAJ5tXRqbd1ZJPDyDEBCKnBEq0YkZ7xEVZhgYoHTNyJ5d0QGwhmhs3o79i/jVfGvM2r7BCM= X-Received: by 2002:a17:906:51dd:: with SMTP id v29mr7289788ejk.69.1604763202257; Sat, 07 Nov 2020 07:33:22 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab4:a4a4:0:0:0:0:0 with HTTP; Sat, 7 Nov 2020 07:33:21 -0800 (PST) In-Reply-To: References: Date: Sat, 7 Nov 2020 15:33:21 +0000 Message-ID: To: Eugene Sidelnyk Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Union `&` operator From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 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.