Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92576 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10736 invoked from network); 20 Apr 2016 20:18:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2016 20:18:10 -0000 Authentication-Results: pb1.pair.com smtp.mail=codekestrel@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=codekestrel@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 74.125.82.50 as permitted sender) X-PHP-List-Original-Sender: codekestrel@googlemail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:35200] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E7/77-14036-104E7175 for ; Wed, 20 Apr 2016 16:18:10 -0400 Received: by mail-wm0-f50.google.com with SMTP id e201so63630536wme.0 for ; Wed, 20 Apr 2016 13:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=dXlwj3kdnbsO6FBsvtKa/+fZKtVAQczMQYe0XM+SBP4=; b=w2fqSpmaVA06jRuOMCjj7geAt4ybgApYnl8kJ6+lFmveGx9EK0lZu7Cut244FZBJd0 8z1C/JWNHpzZCy5AnECs4XN0lRkryH9+ZZ87UG+FfmW2rYUaqbAEwwaN0X01FOqCpI/L DjE0VaFAAyK2ldDyvaFh8MGBAQnkCAhld9XnYfht6NXURbvtqw9zsOBanOMVwbHYQe4K u1KPYhKe1TneaeMwisgbXhhsT3JTQ5JGW0ZK3Cvo+5PFvwqboKBOkTOxYUq18CjvOPAd 2QlP/2FrAFhwGlcQUqaUBZCr8u36K3UY2zFkdldERjqFicn2W6iGFB/AP1vl/58zSIGz p2Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=dXlwj3kdnbsO6FBsvtKa/+fZKtVAQczMQYe0XM+SBP4=; b=lmHbbWZHtgpe+INzmDujMyi9oYgjzvV/XBMAs3ENpqp5uzuOX3eotNNjv0fKztVVcp QdjfzC9Eu82Y+dfUsggbI9wsjDjB76TYX2bH+OaYUCNOuxo1KF9w8MKdAkKG4HxjVjQA MB3GjApMoDMISvFaRm/3PT/EP5J8+B0z8PdD+CXoxuwOE485N6e8CpfzvP9XKxHTe+BC eZGUW6lSzfufmEVquFGCCXHfJshMEfVnXWhS1quEAUa9g3Fmf3Y1lJOFNP9yGv77v1pP D+p1oeKFhyJFMX56AGdBLRtEMEoh8b73FKSIxrcdsTroq4+VI+Kz1on9Ese38R037txO CA1g== X-Gm-Message-State: AOPr4FVCNHJHFs3c8GABxK0cMCf6x7lanbVdwIRxO/Pnc24EfFKB4J/Qh6upEtQK19/oWItJz+AjvZJVx831BQ== MIME-Version: 1.0 X-Received: by 10.194.142.147 with SMTP id rw19mr10487479wjb.31.1461183487249; Wed, 20 Apr 2016 13:18:07 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Wed, 20 Apr 2016 13:18:07 -0700 (PDT) In-Reply-To: <5717DEBD.2090207@fleshgrinder.com> 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> Date: Wed, 20 Apr 2016 21:18:07 +0100 Message-ID: To: PHP internals Cc: Zeev Suraski , Jesse Schalken Content-Type: multipart/alternative; boundary=089e013a2992b5a0510530f04a22 Subject: Re: [PHP-DEV] Re: Improving PHP's type system From: codekestrel@googlemail.com (Dominic Grostate) --089e013a2992b5a0510530f04a22 Content-Type: text/plain; charset=UTF-8 Just a thought that crossed my mind which might satisfy both worlds. Has anyone every considered unions as a type declaration? namespace Vector/TypeDefs union Stringable { as string; as int; as float; private $value; public function __make($type) { switch (type) { case 'string': return (string) $this->value; case 'int': return (int) $this->value; case 'float': return (float) $this->value; } } } my_echo_func("123"); // << A scalar variable on the outside. function my_echo_func(Stringable $stringable) // << a union on the inside { var_dump($stringable as string); // string(3) "123" var_dump($stringable as int); // int(123) var_dump($stringable as float); // float(123.0) } 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 number of "instanceof" type checks as well. On 20 April 2016 at 20:55, Fleshgrinder wrote: > I am not quoting anything because the formatting of your emails is > completely off in Thunderbird. However, I want to add one to the list: > > declare(strict_types=1); > > interface Stringable { > function __toString(): string; > } > > function fn(string|Stringable $arg) { > $param = (string) $arg; > } > > We could add a new *stringable* primitive to PHP though, but do we guys > really want to start doing so? There is pretty much an endless amount of > combinations and finding names alone is endlessly hard. > > declare(strict_types=1); > > function fn(int|string $maybe_bigint) { > // mysqlnd automatically creates the proper type based on the value. > // Expressing this in userland is impossible ... :( > } > > -- > Richard "Fleshgrinder" Fussenegger > > --089e013a2992b5a0510530f04a22--