Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101562 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85402 invoked from network); 6 Jan 2018 19:05:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jan 2018 19:05:39 -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 209.85.128.177 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.128.177 mail-wr0-f177.google.com Received: from [209.85.128.177] ([209.85.128.177:45352] helo=mail-wr0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F7/F1-45945-10E115A5 for ; Sat, 06 Jan 2018 14:05:37 -0500 Received: by mail-wr0-f177.google.com with SMTP id o15so7074412wrf.12 for ; Sat, 06 Jan 2018 11:05:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=djhTUewl2utb829+HZVFIhzJ3+3U7cVtFCjTgYXkQpo=; b=J9lgTJWEX6s/uKamlNDMHl75Na/tajnZEg0xP5ZHREd+MiqWmRGw3YuuLwoX/etn3k FKIjsCL4Boow0oGERjCG8w07jfNKHGmBteQ9dpy6e09VHFEYqQoWCzEADB+HLQzSVN/F e/T1NwZKJG8tQYXNMym5Zvg3eTqUlXndrDlXtSR4KBfX6Je9W2HRdDMeGd4mKq27jJwD 0W4tgf6PA6Ud6CA9JgGsCMoiCzHnOVorpwI7VzLh3s03PbQ9LJhfOwBIYynoW/u7b12f nip0GZStXYZ1p717Laf06wkM5edVZ0x7NJRa8qC4hPZQAhlq11e+FF/FvYp1MpjfX6tu jt0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=djhTUewl2utb829+HZVFIhzJ3+3U7cVtFCjTgYXkQpo=; b=UQQleBiWrPjOOimsOsF0CofQavzBfco0I2RTBtCKUE2O3Zm2xD5T/Gs1tCgtEP3efD W+txbZf3Ns1B+AiiLi4P5bKp+yqAXhfJWU8g2sIlpQuK4jCkPYtzUh9dcuQK1/jaXqKG bdSsV1qsxF+NubGBOmNZSI39WCFXa9LG5j1Hc1cQYevzVvvicPNahO6sThijEQJ5s0St 3NClz7U7jMoz9P7rcTWvFFTT2r2Iw7sKNbJqR4Ez6fCZb//Y/55FRTr86VwzxN24BNyn +sR8hzmVRHc4HtjEgpR9bLdHRptCeSeaXYRnGC5b1OO+aKjkLPg7uFSiIIb9yl07ZJit Vtuw== X-Gm-Message-State: AKGB3mIXcKRgiQrxqkp9QW19T7b1DkLEWWzYJyRwjXwCTKMtwPyvUpAK 3/LIIjwZFmPFKZ2OBHkYWhFtwA== X-Google-Smtp-Source: ACJfBovXWfPkbnU1nialNV1WS4nJ67SsUGx9D4t4XQtGIX7wx9nts3+Ti68lEXmxsMCSW8gCiusxhQ== X-Received: by 10.223.153.144 with SMTP id y16mr6415046wrb.45.1515265533717; Sat, 06 Jan 2018 11:05:33 -0800 (PST) Received: from ?IPv6:2a00:23c4:4b81:ae00:6cdb:7b98:d1eb:7ee4? ([2a00:23c4:4b81:ae00:6cdb:7b98:d1eb:7ee4]) by smtp.googlemail.com with ESMTPSA id o107sm16529301wrc.63.2018.01.06.11.05.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Jan 2018 11:05:32 -0800 (PST) To: internals@lists.php.net References: <9a3a8760-f65a-a5c0-b318-1830a9a986c3@gmail.com> Message-ID: <6f0b56a0-fd97-8926-963e-182eb7b3632d@gmail.com> Date: Sat, 6 Jan 2018 19:05:32 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC][DISCUSSION] Strong Typing Syntax From: rowan.collins@gmail.com (Rowan Collins) On 03/01/2018 23:19, Michael Morris wrote: > I'm not familiar with the Zend Engine as I probably should be. I bring the > perspective of an end user. From what you've posted am I correct in stating > that PHP Type Hints / scalar Type Declarations are in truth syntactic sugar > for asserting the type checks. This is how I've always pictured it, but I've never dug into the implementation before, so I had a look. (If anyone's curious how I found it, I started by searching for "callable", because it's a keyword that should only show up in type hints, then clicked through on LXR to everything that looked promising.) It looks like the actual "assertion" is the function zend_verify_arg_type [1] which calls zend_check_type [2] and formats an appropriate Error if the type check returns false. zend_check_type has to do various things depending on the type hint the user specified, which I'm guessing are classified when the function is compiled: * Null values are checked against nullable type markers and null default values. * A class name traverses up through the inheritance hierarchy of the argument until it finds a match or reaches the end [3], while an interface name has to recursively check all interfaces that might be indirectly implemented [4] * The "callable" type hint has to check all sorts of different formats, and is scope-dependent [5] * Strict array and scalar type hints are just a comparison of bit fields * Weak scalar type hints which aren't a direct match end up in zend_verify_scalar_type_hint to perform coercion if possible [6] When talking about additional type checks for assignment to properties, or "locked" local variables, etc, this is the code we're saying needs to be run more often. For simple types, in strict mode, it's not too bad, but checking classes, interfaces, and complex pseudotypes like "callable" seem pretty intensive. This is likely to get more complex too: proposed additions include union types ("Foo|Bar"), intersection types ("Foo&Bar"), typed arrays ("int[]"), generics ("Map"), and others. So I guess I'm agreeing with Rasmus and Dan Ackroyd that thinking there's an easy optimisation here is naive. For the same reason, I am supportive of the idea of having type checks, at least those we don't have yet, only enable with an off-by-default INI setting, treating them like assertions or DbC, not as part of the normal runtime behaviour. [1] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_execute.c#zend_verify_arg_type [2] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_execute.c#zend_check_type [3] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_operators.c#instanceof_class [4] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_operators.c#instanceof_interface [5] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_API.c#zend_is_callable_impl [6] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_execute.c#zend_verify_scalar_type_hint -- Rowan Collins [IMSoP]