Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97285 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2465 invoked from network); 5 Dec 2016 14:41:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Dec 2016 14:41:33 -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.68 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.68 mail-wm0-f68.google.com Received: from [74.125.82.68] ([74.125.82.68:33582] helo=mail-wm0-f68.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C6/92-11772-B9C75485 for ; Mon, 05 Dec 2016 09:41:32 -0500 Received: by mail-wm0-f68.google.com with SMTP id u144so16313330wmu.0 for ; Mon, 05 Dec 2016 06:41:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:subject:from:date:to:message-id; bh=UxDf6iqJQzhuaLmMUenPIuHXEhpWe7XKjLz2r+zkyvQ=; b=EMzphsQ343kwhydomUvo5K3ZedJESmL6bxNtPVaVc9Db9V4Bp3eqk6mwZFNamG3Zbg BuRRHw0La1mCSgvzE1L+OD4aC1Vvf0uHhD76FgN3MWvE8dO9PyZrHZnz7/+ScLWFIVTu Qy4LobvthLrJeROCHJhon4RE6jf7iAVsIQQLiokczc6n0LG2HhD1giTCfE4eMGfesOOW 3RyDzCfVl6XdYWCKi6ll+xi5+b2Q9Shud8vf+R1mtMLIY07cx81GRH7wKwHx602FKL9h DlF2LigkSieibcglqrT9mFN5Vagp/VpvTXkwHxnzDqkGewkEk+4yx2FOd7N1xpmMQta1 DhDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:user-agent:in-reply-to:references:mime-version :content-transfer-encoding:subject:from:date:to:message-id; bh=UxDf6iqJQzhuaLmMUenPIuHXEhpWe7XKjLz2r+zkyvQ=; b=YUkuL/sOxb/GjS5p5/OwmsaK0oGfNKd3Gb3zutO8y32OGVhWuQrUp/BKkKrrqw1XDN gHA2asDy0UHPHcULYnKR3nYrJJ1vHY3Y6RL47IDpG6yTydl+3mRTR/ZVvGVQrSU8/6Ke iW5MPGkTBkPsgFPGdU0BQRibyngS1vZXid6P1QYV5Gzxj++L7r2VhAjvxGSdJcPXwkEv 9WfoZ+5jliZX1mHfeouYWuJ16Y2G8EgKyKVdkwYE+arlpaRw/O7Ae+HyJ9ep9FYtMQlF 6TyjfrEqH9Sg0UqohVKigt0xRcgEfaPA1azVSl3tPj4NpJQ4M6F0cSm8y/94ESlKZpAI G5ng== X-Gm-Message-State: AKaTC03NTZOR34qP4yAsMlvZUzp4uKiIerF7TodYyEO9MPEgWAA+BBtMW+pyujMyTam8CQ== X-Received: by 10.28.189.196 with SMTP id n187mr10602068wmf.133.1480948888109; Mon, 05 Dec 2016 06:41:28 -0800 (PST) Received: from [10.92.122.164] (94.197.120.161.threembb.co.uk. [94.197.120.161]) by smtp.gmail.com with ESMTPSA id 191sm458018wmr.11.2016.12.05.06.41.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Dec 2016 06:41:27 -0800 (PST) User-Agent: K-9 Mail for Android In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Mon, 05 Dec 2016 14:41:24 +0000 To: Alex Bowers ,PHP Message-ID: <675433DE-3876-40A9-852D-C8A0368C1D96@gmail.com> Subject: Re: [PHP-DEV] [Concept] Magic Casting From: rowan.collins@gmail.com (Rowan Collins) On 2 December 2016 13:37:20 GMT+00:00, Alex Bowers wrote: >Hello All, > >In PHP we currently have the ability to type hint classes in method >signatures, however, sometimes it would be useful to convert the items >to a >different instance / type. > >An example is collections and arrays. If implemented properly, an array >and >a collection could be used interchangeably, although a collection may >be >preferred. For consistency, having the ability to dynamically cast from >an >array to a collection would be very useful. I think there are really two different features wrapped up here. First, allowing user defined classes to define casting behaviour to or from other types. This has certainly been discussed in different forms over the years, although mostly in terms of casting *to* another type (e.g. a generalisation of __toString). One of the big challenges is that without method overloading, how do you nicely declare two different casts, to or from different types? The generalised case also suffers from the problem of multiple despatch - do you call Foo::fromBar() or Bar::toFoo()? The second part is making that casting automatic when a type hint is encountered. I can sort of understand this as a generalisation of coercive type hints on scalars, but it feels like it could get rather hard to debug. It seems easier to read in your example to just type hint the collection, and make the user manually create one: function whatever(Collection $c) { ... } $a = array( ... ); whatever( Collection::fromArray($a) ); Explicit casts would take you from there to: function whatever(Collection $c) { ... } $a = array( ... ); whatever( (Collection)$a ); Either of these is more typing than the cast being implicitly added by the engine, but it's clearer that there's some conversion happening. Imagine if the cast is an expensive operation - calling it explicitly over and over again would be an obvious clue to refactor, but having it silently called repeatedly might go unnoticed. Regards, -- Rowan Collins [IMSoP]