Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79181 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28291 invoked from network); 25 Nov 2014 21:43:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Nov 2014 21:43:30 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.48 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.218.48 mail-oi0-f48.google.com Received: from [209.85.218.48] ([209.85.218.48:52817] helo=mail-oi0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C0/4E-40624-108F4745 for ; Tue, 25 Nov 2014 16:43:29 -0500 Received: by mail-oi0-f48.google.com with SMTP id u20so1082457oif.7 for ; Tue, 25 Nov 2014 13:43:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=Rp3eMMdoVi6SFoSFc9799KVSpPKT/+VizXEt8JlTMSo=; b=Qm1ZRUs0eLa0Rrlt41PyMWyHOdQY2PxB6XukzCMpiekzVOE5UtBnulTzguSPJ8eABS v2H0D+LGjXoJkjQUoyEIFDV/vr4gy52ALjW8eBK21EvjbGRl8s8NXOpiO6JnCid5aios zxl/1OGjDYB+7p/XiF6J4XylZMSwVOwimDhCXtuipKQ8SWSbrLLaFC9CI+IeZdF0NAnx zuv8abE5xHqtBbDLF0ec6nZhAxffLvHgmbJsNRfsKb/L0DiJw8/hr2ZLgB0Qd/aQ6IaY Xjt+UNK/tVL9BuXjvBIQckXkwoXThWJ69jtsg5ODdEQgdTZwt46jmRjgUUrNfmggHk9A LS+A== MIME-Version: 1.0 X-Received: by 10.60.35.132 with SMTP id h4mr18051484oej.12.1416951806635; Tue, 25 Nov 2014 13:43:26 -0800 (PST) Sender: morrison.levi@gmail.com Received: by 10.76.89.237 with HTTP; Tue, 25 Nov 2014 13:43:26 -0800 (PST) In-Reply-To: <5474EF98.8070602@mabe.berlin> References: <5474EF98.8070602@mabe.berlin> Date: Tue, 25 Nov 2014 14:43:26 -0700 X-Google-Sender-Auth: 6HC8s1DVVlmK7QyYpXNdJJNqXn0 Message-ID: To: Marc Bennewitz Cc: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [RFC][Discussion] Return Type Variance Checking From: levim@php.net (Levi Morrison) On Tue, Nov 25, 2014 at 2:07 PM, Marc Bennewitz wrote: > I think it's required to do the type check on runtime (Option 2) because > one of the use cases for return type-hint are factories and such often do > instantiation in base of unknown string values: > > class MyFactory { > public static function factory($name) : AdapterInterface { > $class = 'MyNamespace\Adapter\' . $name; > return $class(); > } > } It seems that I did not explain this clearly enough; I apologize. The variance has to do with the declared type in the function signature when inheritance is involved, not the type of the value returned by the function. For instance, under any of the three options this code will work just fine: class Foo {} class Goo extends Foo {} class FooFactory { function create(): Foo { return new Goo(); } } As long as the return value from FooFactory::create returns Foo or a subtype of Foo (such as Goo), then it will work. The variance that is under discussion in this thread is about the declared return type in the signature: class GooFactory extends FooFactory { function create(): Goo {} } In this case, GooFactory::create() declares a return type of Goo, which is a subtype of Foo [the return type of the inherited method FooFactory::create()]. This is a covariant return type. If we choose option 3, the only possible return type for GooFactory::create is Foo. Hopefully this clarifies the issue.