Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92592 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66585 invoked from network); 21 Apr 2016 11:26:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Apr 2016 11:26:02 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.53 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.53 mail-wm0-f53.google.com Received: from [74.125.82.53] ([74.125.82.53:35438] helo=mail-wm0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CA/1F-14036-8C8B8175 for ; Thu, 21 Apr 2016 07:26:01 -0400 Received: by mail-wm0-f53.google.com with SMTP id e201so83110226wme.0 for ; Thu, 21 Apr 2016 04:26:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to; bh=jdw5X0w4QqWIEKVaRj82722o3WwbcTbZGeea8oKwtQw=; b=TfqY5MetHhp/QdzUxkzKw4AYyb8/RBudzQbumaWGKZUPSRj6tuVCgKM9TsV/7iH3IC 1w/CE4QHfGi/2aYvTmrDHdix1ljYc37PhtLTV+fJbSiP0mjH+0x9f/h0aDAAfaMTNcrB WpsHox/I3MLDykXGrLZghagmnrR7Ub6ISNtS2bNSjn5zlEewknZdVVnkOA2eIiZjzSX7 fye/CKKXPJZrI6WHoMWiaxBO92YI/y1zHjEFTadA+GEZe2lHuo7LtkcQZu6CXuW0uBFr dvQS60LsxnD6l8s6Yl58o+a3jz8XUVha0OfLEDUr53UMIOx4/pVa4lXakNrrsT6K0WTm oQpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to; bh=jdw5X0w4QqWIEKVaRj82722o3WwbcTbZGeea8oKwtQw=; b=Vc1vFF7Os6TEqqGUw6A7e/400/y+tlFLMfAE2kMooDLa3M2AB2o8c1w+yWmvwdLcx6 fGKkNuMPqpsyUYHujjTD98yHlmY0YAE1mZoBDu4/LCi+RDufvzYo4h4s+gX8I3qwEoci z4530EcBI+krYNnpnaUAFATHXGi9d2DuiDlz82dh8QUvqc3lQ4ptcz3Y1CRQ8zBCQNTA RheGMLnEskITDejgAo5PIdbShfFwQrvz/E+aVi7xHuB3tec6n9W40iua/8AA9zOSgLQL JqYsM7ZcJl/aC5jdo/ku+ICDxUXVxY50GosgJeAZwQ7uTeb/8/rgQ0C3loSzHv4xJtGo yO1A== X-Gm-Message-State: AOPr4FUXNqb4AGAAC9Qvp0Iqs6BdPFs4fLta1LwYuxAxxFd3CNss8VQMUS3wv8WIU2I3JA== X-Received: by 10.194.103.98 with SMTP id fv2mr14617691wjb.23.1461237956760; Thu, 21 Apr 2016 04:25:56 -0700 (PDT) Received: from [192.168.0.77] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id x2sm2359628wjr.33.2016.04.21.04.25.55 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 21 Apr 2016 04:25:55 -0700 (PDT) To: Jesse Schalken References: <3cc8a4c7-2640-11ae-a67b-06f909ac1e27@texthtml.net> <57173859.4080501@rochette.cc> <57180921.8020000@gmail.com> Cc: PHP internals Message-ID: <5718B85E.5090807@gmail.com> Date: Thu, 21 Apr 2016 12:24:14 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------080203060908050504060307" Subject: Re: [PHP-DEV] [RFC:generics] From: rowan.collins@gmail.com (Rowan Collins) --------------080203060908050504060307 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Jesse Schalken wrote on 21/04/2016 04:18: > > Sticking with your example for a moment, if the type parameter is an > "int", then the only type that can /currently/ be expressed in the > type language that is a subtype of "int" is "int", so it's not very > useful /right now/. However, a possible introduction of integer > literal types would change that, especially in combination with > unions. This would enable you to express the type "1|2|3", i.e. only > one of those three integers. This would be useful to typecheck enums, > which are typically an "int" or "string" which only accepts a finite > set of values. As such, the type parameter "T is int" could be filled > with "int" or "1" or "0|1|2". (Imagine a class "Bar" and > various subclasses which each must select a single integer as kind of > tag, "Foo extends Bar<1>", "Baz extends Bar<2>" etc. I do this in > TypeScript with string literal types all the time.) I'm struggling to follow this example; it seems like you are using the type parameter to supply actual data to the implementation, which goes rather beyond generics, and into full-blown template metaprogramming. I'm not sure why you'd ever need the sub-class to compile in a value that way rather than just overriding a method somewhere, and would need some convincing that PHP needed this kind of complexity. > Beside from that, the introduction of any one of union types, > intersection types, generic array types or callable types would mean > the type constraint for a generic type can usefully be something > besides a single class/interface. For example, if " array|Travarsable>" were written "" I > would expect to be able to do "$t instanceof array|Traversable", but I > wouldn't be able to unless "instanceof" is intended to and is extended > to support arbitrary types. This is a stronger argument, although I think extending the instanceof operator might be a good idea in that case anyway - otherwise, we would have a constraint that you can assert (produce a TypeError at a function boundary) but not easily test for. To avoid replicating the engine's entire checking routine, you'd end up doing this: function ugly_instance_of($value): boolean { try { return (function(T $checked) { return true; })($value); } catch ( TypeError $e ) { return false; } } if ( ugly_instance_of>($x) ) // if ( $x instanceof array ) *shudder* ;) Regards, -- Rowan Collins [IMSoP] --------------080203060908050504060307--