Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102865 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26411 invoked from network); 16 Jul 2018 18:17:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2018 18:17:59 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 64.147.123.21 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 64.147.123.21 wout5-smtp.messagingengine.com Received: from [64.147.123.21] ([64.147.123.21:46343] helo=wout5-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 09/21-39793-351EC4B5 for ; Mon, 16 Jul 2018 14:17:58 -0400 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id BFC4E38F for ; Mon, 16 Jul 2018 14:17:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Mon, 16 Jul 2018 14:17:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=6ywZzVFOXnJhUj3kdD8AOsujnX20t EMnyi6njRhyQF8=; b=PDITPR+kohXHbQr5gMzDwGO/4CABbDZNi8l9mpjskGZEW HetJYSCDZHlmuRzWINPBJcewUgz1+RDy+RDj8h9Jl5AcaxHYS+V+Jtk+Chiyu9Hb uZdAjG6p9SKj2m9OPiXHiB8SLlDRpaOKJJfBFN9qatxiXIJXG62YU+RwxRP30bK0 LgebzNxrA1gcZhXACp5z914t5POefi0+twYgwwDDnMvJ5+tN+pDd7vvvFAp97+6C SpPgagfsK6j5LQoGdsRwwLpvnHqVWG5oL69I6XT/4IR4N3o2peYjWkBKzrgZ0I9Q FiZ2NoWIowgkUlI7+tTIL1yM/meppkXZ4ZJPMlglA== X-ME-Proxy: X-ME-Sender: Received: from vulcan.localnet (216-80-30-152.s3222.c3-0.frg-cbr1.chi-frg.il.cable.rcncustomer.com [216.80.30.152]) by mail.messagingengine.com (Postfix) with ESMTPA id 942E51026A for ; Mon, 16 Jul 2018 14:17:51 -0400 (EDT) To: internals@lists.php.net Date: Mon, 16 Jul 2018 13:17:50 -0500 Message-ID: <1599498.ygS5CM8mOq@vulcan> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart8465036.BZJ66JjviO"; micalg="pgp-sha512"; protocol="application/pgp-signature" Subject: Re: [PHP-DEV] [RFC] Optional typehint check for parameters From: larry@garfieldtech.com (Larry Garfield) --nextPart8465036.BZJ66JjviO Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Monday, July 16, 2018 12:23:42 PM CDT Zeljko Mitic wrote: > On Mon, Jul 16, 2018 at 1:42 PM Rowan Collins > > wrote: > > On 16 July 2018 at 12:06, Arvids Godjuks wrote: > >> Basically, you went wrong when you proposed a switch that controlls > >> language behavior. To add to that - a switch that probably is not > >> controllable by code that is running. > > > > While I agree with your general principle here, I would like to raise one > > point in this proposal's favour, which is that PHP's type hints are > > effectively assertions - if the value is not of the given type at that > > point, an error is raised. Assertions are always something you turn off in > > production builds, with a global switch, to the point of compiling the > > code > > as though they don't exist, so it's not unreasonable to have a similar > > switch to disable run-time type checks. > > > > As Nikita points out, this is not true of non-strict scalar hints, so that > > does raise a complication. > > > > I was going to point to Dart as an example where type checks have been > > implemented this way, but it seems that version 2 has replaced the > > "checked > > mode" with stronger static analysis, and run-time checks are automatically > > inserted only for cases where the safety can't be inferred. > > > > This might actually be a more promising optimisation for PHP - perhaps > > OpCache could mark certain code paths as type-safe (or maybe it already > > does?) As an exaggerated example, this code only really needs to check the > > value's type once, not four times: > > > > function foo(Bar $bar): Bar { > > > > return $bar; > > > > } > > function test(Bar $bar): Bar { > > > > return foo($bar); > > > > } > > $something = test($something); > > > > Regards, > > -- > > Rowan Collins > > [IMSoP] > > Exactly. Languages like Java and Hack will not compile when sent parameter > is of wrong type. Once all is OK, generated code will never check that > value again. > > My suggestion is something between; if during development my code works, I > don't ever need type check again. That fails with the potential for dynamic typing. $class = derive_class_name($_GET['some_input']); $foo = new $class(); doStuff($foo); function doStuff(BarInterface $b) { ... } In PHP you *cannot* know with certainty that all code is type safe at compile time. Removing type checks at runtime will *always* lead to reduced safety and at best less useful error messages. That's just the nature of the language. PHP is not Rust (as cool as that would be). It sounds like your main argument for why to even bother with this is performance. I think you're greatly over-estimating the performance impact of type checking. I wager if you just cache a few database lookups and you'll get a much larger performance win. For far, far less effort and confusion. --Larry Garfield --nextPart8465036.BZJ66JjviO Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE/ph/GXfY8v0YwFBp4MMKwDFxYXcFAltM4U4ACgkQ4MMKwDFx YXcWCAgA62rB/4mPzgtWMPlU8qu96VjK+b+uc9ZPN7SwCgTw74mvKeLNI2/ljfK/ vWvfuc1lMeMl1WRAW5cVOaLgzr/oN68SeEj+4197JhDfung3iA7KqtoDu3NFB8AT Jgjhi0/i2Nq2Yzr48bn3Z8uHRKqRfzfy1JKwahXfnccKHs3HfFW1PN84ItHEeoSB ELTHV2M60cpmGK7Wf6bEGuGYXemCUcXxJkLMZBGp5asiibjPOZM8a+7LXBg0fmtL dIO4sDvmbFRLRLS3TWVsoyRcyrULCG9Vf1KJf8AXHIuxn3oYxE6dwWgllM4LmHjD BK2513zLSYgmsqZ3Ea81qA/kvWPdtg== =HRAU -----END PGP SIGNATURE----- --nextPart8465036.BZJ66JjviO--