Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81818 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27253 invoked from network); 4 Feb 2015 15:32:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2015 15:32:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=danack@basereality.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=danack@basereality.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain basereality.com from 209.85.212.176 cause and error) X-PHP-List-Original-Sender: danack@basereality.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:40184] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 30/40-22545-B8B32D45 for ; Wed, 04 Feb 2015 10:32:28 -0500 Received: by mail-wi0-f176.google.com with SMTP id bs8so32716094wib.3 for ; Wed, 04 Feb 2015 07:32:24 -0800 (PST) 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:content-type; bh=ExiFthR+Z4WwAUOJ6rFAULSgJ/MUBHZsGAyGJznbGQg=; b=SGBxW75un+cdLikwiefBH0XZNu71AZ8CxyT+KX6gLBtCOZvSI1MQGYQDUb8Ldu/UJq uckZwgvEJAZWZVg1b/J4uruH00ZoIYn2MmFk5OU8OEgNzK4S1/Fw/P/w+KdaN9pAhF8a lT96hB/EKrOGHEVkiFS+k4i101HwQHTWctSv2iMdNe7v0zGWFhWgSJlnZrdBJ7OM6Jee ke3j9ury11shMgsVpz8Lx4GDa7tB/XEg7kpjkuFXnKrlYf/CkYFV5MZ90S9goPAR0l8V z81fxTaylXBdTW8Q9F22iYBZ0S9rkfY+sS35VqU72MC3dCV2AGT2J1AbmQKot0F+jh8T 9J7w== X-Gm-Message-State: ALoCoQmbzeoG8vkryVU98WdNW6s9h62KA9dFrkUs14XkJEhXmWRD999T+uQ6o8424bbnTiWp28Bv MIME-Version: 1.0 X-Received: by 10.194.236.200 with SMTP id uw8mr68227891wjc.10.1423063944439; Wed, 04 Feb 2015 07:32:24 -0800 (PST) Received: by 10.217.82.208 with HTTP; Wed, 4 Feb 2015 07:32:24 -0800 (PST) X-Originating-IP: [78.149.8.112] In-Reply-To: References: <00c101d04049$ca411ec0$5ec35c40$@tekwire.net> <54D1CA7C.8060204@php.net> <54D1E6EC.7060702@php.net> Date: Wed, 4 Feb 2015 15:32:24 +0000 Message-ID: To: Dmitry Stogov Cc: Sebastian Bergmann , PHP Internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: danack@basereality.com (Dan Ackroyd) On 4 February 2015 at 10:13, Dmitry Stogov wrote: > This won't fit into "loosely-typed" PHP, and will lead to writing - > sin((float)$x). Hi Dmitry, I think what people miss is that the place where the conversion from an unknown type to an int takes place, is also the place where you would put the human readable reason for why a certain type is needed: function placeOrder(int $amount){...} // Throws an exception with the provided message // if the value cannot be converted to an int. function getInt($value, $exceptionMessage) : int {...} function processOrderRequest() { $amount = getInt($_REQUEST['amount'], "Sorry, the order amount must be a whole number."); //Yay, our IDE can tell that $amount is an int if the code reached here. placeOrder($amount); } It's this join point between layers of code that make explicit strict scalar types be so useful. On one side of that join point we don't know (and shouldn't care) what the details of the value are. On the other side of the join point we know that it is an int and don't have to consider any other types. I would love to have strict type hints in PHP, and this may sound contradictory, but they would almost never ever get triggered in my code. The only time they would be triggered is when there is a mistake and I have missed converting something to the expected type. Having the scalar type defined on the parameter allows for code analysis tools to find errors. This would make cases where I forget to convert them properly be almost non-existant. With asserts inside the function those errors would only be detectable at run time. btw For your exact example `sin((float)$x).` this is actually a case where another scalar type of 'number' which is satisfiable by either a float or an int would be useful, but that's definitely a step too far before we have any scalar type hints. cheers Dan