Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93722 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2835 invoked from network); 2 Jun 2016 17:35:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jun 2016 17:35:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.47 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.47 mail-wm0-f47.google.com Received: from [74.125.82.47] ([74.125.82.47:34864] helo=mail-wm0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A7/26-62101-06E60575 for ; Thu, 02 Jun 2016 13:35:29 -0400 Received: by mail-wm0-f47.google.com with SMTP id a136so241003898wme.0 for ; Thu, 02 Jun 2016 10:35:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=gmLCV39QxRuugNylffdVdFzrDEvj/6KTFPXgzybbcMc=; b=Vb4Z8Jpat8cVMxmPPyuVlItH6afCzfzwRL2eOZ6znpbUxwRXUVMu61TvkmZhUJ3S8w UlnMsU2E94horlhxF63uy97a9fx2GRO4+6SIUP+TwoHM0SSVueMBFP0z2rnd1L2f+HPH T6en9JEWTtOuFSU6z0pJqRWO4sQPNEVAfuLMjW7duaM/L/8ObrsW64KxZwMBhOue7D0v P0pQF3BR3NbwhMVPOQ6dHY6zCT3fJHONhjOhbbQac0QQ24lfm5/Sw4Ml1cnxU7mnzIxQ XaaxpssoDnn5OccvcA3mCovmKRoNnSBagCB1UUusW/edXj9MljZKYh0PIX15a59EnONT XW0Q== 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:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=gmLCV39QxRuugNylffdVdFzrDEvj/6KTFPXgzybbcMc=; b=lTjf7ym7wGEqSAcafEIYe9jel63WbucJBzHyjGZYJ4QETV1fEmh4WJAX4t0zNxvKQL e2pGIztOyC5V+PF0i2Aqxu5U4G7i9IOPv8NKh1fMutlFMrgQ8COhcFdtqrmQm+FKQLtI N4QD4YmSylb3j62PpfKq/u0LD5y3HKRYB7PP1hDuLtCwD4rjKaW9eLxAixOPfVfkjwBi y0OsFH/tmDy4LZIGzwhkmEmeswAM8Zhe/IIyMsBjVHzc5n91k8TomlLUPumpEaaF8qU7 h4zH5gkEKP+qiEZ0bCJwJGSN7d5rqpUX0yJgkyDmTAu/vMwPV0hD/AbibzF0CYPEUA8C sKmg== X-Gm-Message-State: ALyK8tJdh3KQIAgX2bLItj/ttym2n97kXwbB5THKMm9aFJHPucHjeuwC7eQc4ZG4tKLajw== X-Received: by 10.28.150.86 with SMTP id y83mr10373377wmd.24.1464888925757; Thu, 02 Jun 2016 10:35:25 -0700 (PDT) Received: from [192.168.0.98] ([93.188.182.58]) by smtp.gmail.com with ESMTPSA id l9sm1644901wjm.0.2016.06.02.10.35.24 for (version=TLSv1/SSLv3 cipher=OTHER); Thu, 02 Jun 2016 10:35:25 -0700 (PDT) To: internals@lists.php.net References: <0A.C5.62101.1C860575@pb1.pair.com> Message-ID: Date: Thu, 2 Jun 2016 18:33:04 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <0A.C5.62101.1C860575@pb1.pair.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: [RFC] [PRE-VOTE] Union types From: rowan.collins@gmail.com (Rowan Collins) On 02/06/2016 18:11, Andrea Faulds wrote: > That aside, the behaviour the RFC now specifies for how weak typing > interacts with union types is frighteningly complicated. I don't see how > it could be anything other than that, but the new complexity this > introduces to PHP is enough for me to vote against this RFC, even > ignoring my other concerns. That is rather a long-winded set of rules, I agree. I wonder if it's possible for the order the union type is defined to be taken into account? That way you could specify the rule thus: * The type is tested against each type in the union in turn. * If weak typing is active, scalar types will be coerced where this would normally be allowed, and passed over where a TypeError would be thrown. * If strict typing is active, scalar types will be treated the same as non-scalar types, and must match precisely. So: function f(int | float $number) { echo gettype($number); } f('10'); // int function g(float | int $number) { echo gettype($number); } g('10'); // float function h(int | Foo | string) { echo gettype($number); } h('10'); // int h(new Foo); // object h('hello'); // string function i(string | int) { echo gettype($number); } i('10'); // string i(10); // string This last example is the only one that might be a bit surprising - the "| int" in the type def is actually doing nothing - but there's always scope for user error. In strict mode, things would be a bit different, because no coercion is allowed: declare(strict_types=1); function f(int | float $number) { echo gettype($number); } f('10'); // [!] TypeError function g(float | int $number) { echo gettype($number); } g('10'); // [!] TypeError function h(int | Foo | string) { echo gettype($number); } h('10'); // string h(new Foo); // object h('hello'); // string function i(string | int) { echo gettype($number); } i('10'); // string i(10); // int It may be that the implementation makes this prohibitively difficult, but it would certainly be easier to document than the rules currently proposed. Regards, -- Rowan Collins [IMSoP]