Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92604 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 413 invoked from network); 21 Apr 2016 19:19:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Apr 2016 19:19:57 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.84 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.84 mx103.easyname.com Received: from [77.244.243.84] ([77.244.243.84:42367] helo=mx202.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 73/44-14036-BD729175 for ; Thu, 21 Apr 2016 15:19:55 -0400 Received: from cable-81-173-133-226.netcologne.de ([81.173.133.226] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1atK8l-0004Gd-TO; Thu, 21 Apr 2016 19:19:52 +0000 Reply-To: internals@lists.php.net References: <5B147E88-CC0A-4CBC-A49D-C7FE3BF557C0@zend.com> <6F.C3.12455.94C5F075@pb1.pair.com> <20160414094440.GF19347@phcomp.co.uk> <570FD94F.90703@fleshgrinder.com> <570FE8A9.4020809@gmail.com> <20.53.29891.17401175@pb1.pair.com> <1461145606.7556.11.camel@kuechenschabe> <8D27A75B-E705-49E1-8ACD-B95E4DEDA383@zend.com> <5717DEBD.2090207@fleshgrinder.com> To: Dominic Grostate , PHP internals Message-ID: <571927C7.7060801@fleshgrinder.com> Date: Thu, 21 Apr 2016 21:19:35 +0200 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/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="EGusenoxhLoKTn9u8mhoCGrTHsdwlVbSF" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] Re: Improving PHP's type system From: php@fleshgrinder.com (Fleshgrinder) --EGusenoxhLoKTn9u8mhoCGrTHsdwlVbSF Content-Type: multipart/mixed; boundary="0kaJnjUhQgfUfX0d6iOoBLo9wxfolh0GX" From: Fleshgrinder Reply-To: internals@lists.php.net To: Dominic Grostate , PHP internals Message-ID: <571927C7.7060801@fleshgrinder.com> Subject: Re: [PHP-DEV] Re: Improving PHP's type system References: <570EA5EB.8090501@fleshgrinder.com> <570EAB0D.6080706@gmail.com> <570EB67E.8010908@garfieldtech.com> <5B147E88-CC0A-4CBC-A49D-C7FE3BF557C0@zend.com> <6F.C3.12455.94C5F075@pb1.pair.com> <20160414094440.GF19347@phcomp.co.uk> <570FD94F.90703@fleshgrinder.com> <570FE8A9.4020809@gmail.com> <20.53.29891.17401175@pb1.pair.com> <1461145606.7556.11.camel@kuechenschabe> <8D27A75B-E705-49E1-8ACD-B95E4DEDA383@zend.com> <5717DEBD.2090207@fleshgrinder.com> In-Reply-To: --0kaJnjUhQgfUfX0d6iOoBLo9wxfolh0GX Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 4/20/2016 10:18 PM, Dominic Grostate wrote: > Just a thought that crossed my mind which might satisfy both worlds. H= as > anyone every considered unions as a type declaration? >=20 > namespace Vector/TypeDefs >=20 > union Stringable > { > as string; > as int; > as float; >=20 > private $value; >=20 > public function __make($type) > { > switch (type) { > case 'string': return (string) $this->value; > case 'int': return (int) $this->value; > case 'float': return (float) $this->value; > } > } > } >=20 > my_echo_func("123"); // << A scalar variable on the outside. >=20 > function my_echo_func(Stringable $stringable) // << a union on the insi= de > { > var_dump($stringable as string); // string(3) "123" > var_dump($stringable as int); // int(123) > var_dump($stringable as float); // float(123.0) > } >=20 > Perhaps not exactly like this, but adding unions as type of class > declaration should save a hell of a lot of keywords, and may save a num= ber > of "instanceof" type checks as well. >=20 I do not like the idea because these types directly become part of your public API and you end up including a dozen packages just to get the basic union types in. More IO, more cache utilization, more dependencies, ... Also, one cannot change any type that was ever declared without a direct BC. No, sorry, I thing that this is very bad. :( However, the idea to make objects castable to more of the various scalar types is not new and would be awesome. interface Object { function __toBool(): bool; function __toFloat(): float; function __toInt(): int; function __toString(): string; function __toArray(): array; function __toObject(): object; function __toResource(): resource; } Auto-conversion only if not ambiguous (`$o1 + $o2` where both have `__toInt()` and `__toFloat()` throws an ?Error but `(float)$o1 + (float)$o2` works so does `intdiv($o1, $o2)`), next is operator overloading. ;) --=20 Richard "Fleshgrinder" Fussenegger --0kaJnjUhQgfUfX0d6iOoBLo9wxfolh0GX-- --EGusenoxhLoKTn9u8mhoCGrTHsdwlVbSF Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXGSfKAAoJEOKkKcqFPVVr3IsP/Aq7/RrwbnusKxGoHM9bDbM/ 5TzajAyRHgSyVIg7/Sqig6tDKimP1iAJgKXEk/PH0589RhFNEi1o6gBGOkgzCUnS W0MbiWHhOBLFHVBekl2G9J9vNDQSi3ztBGbozEkI5Q4uBot7CElRvh8k7LJb0FCD trIpZlELf7NHIjqCN+o0LWVSATxnUSagDF748tyeArFSoDOApUmFpr6Ypr/El/8E 3STOACXTzqVOORIzf7+QgQU+qsKCXtathcYWRjq9OXzCSVgr44MtDSmyK/BLrkY1 0O71HcG7NyD/+luRSlu40geY7cjQDHfe/rm53j3XXePAxRuvXs6ZkZLCIpL088sM h1mDkbd+3UMX3tX16PnEE+3O87dKKfaV2GNRkNawxUQCZ5mEBdUCB0cMXnHwu9bn uISI4ufWyd5ESt+0OSr2S9d1g21C7P9R1jH+kYdibOFb93qEYfUvJu0zA9Hcjn0+ NMAnDlXGt3LF4gXLv6FQZxmpmwOUInpmPYviw2dDnhUpdJcE6oFr4NplGT4HEBxc kgg53Be0FKJa83aXsp1/t8VH6MeDdIyVp7QURtupupnkDVCi1EFDtkP7YGypsmhR o5NTt3yYt8VY8HqFTZVaGShp/BX5QrV+QYOi77i8nbuHTz1iMNvKPns+IZp+goGY aqPfifJ7NS/Xl+9MUm+l =v6uJ -----END PGP SIGNATURE----- --EGusenoxhLoKTn9u8mhoCGrTHsdwlVbSF--