Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75549 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 87957 invoked from network); 15 Jul 2014 15:05:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2014 15:05:51 -0000 Authentication-Results: pb1.pair.com header.from=pjsturgeon@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=pjsturgeon@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.41 as permitted sender) X-PHP-List-Original-Sender: pjsturgeon@gmail.com X-Host-Fingerprint: 209.85.215.41 mail-la0-f41.google.com Received: from [209.85.215.41] ([209.85.215.41:51453] helo=mail-la0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FD/B1-15121-E4345C35 for ; Tue, 15 Jul 2014 11:05:51 -0400 Received: by mail-la0-f41.google.com with SMTP id s18so777562lam.0 for ; Tue, 15 Jul 2014 08:05:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=PB8AVafS3NnjWsu435LvNdKmLtTLxROSPc9bk/P5Oog=; b=rimpO+rppFijtaW6+qZESbDcuCIgU3H/gUYQiZTmiWm2BITIcLrbTLGd3ZpvVWw1yY l2jSKXZrDw3Z0gLRyHA7/AUvp/GX88CNShr+we6KNNUHc1A6amnRVqi9HgymcP/JTDBe Es/ECukpuhKePrmdhM9e7x3foW9I+yUtqD9tfvzS/9c/hECmfZnPKkpV7n+vaPeh7fWm GmACAwQL/A7KHU5b6lO0OaRoUCESfNuMcBCPLEPoNhV0LeNUf497zVNxXWC1AHWft/2M 8YGaAInLKmZXnPG4IolsnCDTFuXiF6P+yDBKoculXdaTFIpWsZSHSJy5kT+xGgGl9EPO KUjw== MIME-Version: 1.0 X-Received: by 10.152.202.197 with SMTP id kk5mr19851866lac.19.1405436747913; Tue, 15 Jul 2014 08:05:47 -0700 (PDT) Received: by 10.114.168.70 with HTTP; Tue, 15 Jul 2014 08:05:47 -0700 (PDT) In-Reply-To: References: Date: Tue, 15 Jul 2014 16:05:47 +0100 Message-ID: To: Park Framework Cc: Chuck Reeves , PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: pjsturgeon@gmail.com (Philip Sturgeon) On Mon, Jul 14, 2014 at 11:16 PM, Park Framework wrote: > Maybe we should make two different syntax? > > fun(int $num, array $list) - for strict type compliance > fun((int) $num, (array) $list) - converted to type > > It will be very obvious, and without magic. > > > 2014-07-14 23:23 GMT+03:00 Chuck Reeves : > >> I am new to the list and internals so my question might be obvious to >> others: Why even cast the variable? When I look at a function and see: >> >> >> function myFunc(array $someData) >> { >> //some processing >> } >> >> I understand that myFunc requires an array passed in. If I try to call >> myFunc like so: >> >> myFunc('foobar'); >> >> I get a fatal error stating that I did not pass in an array. If I try w= ith >> stdClass: >> >> myFunc(new \stdClass()); >> >> I will get the same error however If I cast the object before passing in >> like so: >> >> myFunc((array) new \stdClass()); >> >> It works just fine. The benefit of having the hint tells whom ever is >> going to consume the function that the parameter needs to be casted to t= he >> scalar type. >> >> If I have this: >> >> function addTwoValues(int $valueOne, int $valueTwo); >> >> It is clear that i need to make sure I'm using int's. If I try to call >> that function with any other type, I would like to get the error instead= of >> some kind of auto-magical cast into something that could product strange >> results. >> >> I think the benefit is in having the hint with excluding the cast of the >> value. Let the user cast the value to what the function requires. IMHO >> Allow the author of the function to dictate what is needed to execute. >> >> >> On Mon, Jul 14, 2014 at 3:59 PM, Andrea Faulds wrote: >> >> > >> > On 14 Jul 2014, at 20:53, Rowan Collins wrot= e: >> > >> > > The debate in this thread basically comes down to us each wanting ou= r >> > favourite from that list of features to have the privilege of dedicate= d >> > syntax in function signatures. >> > >> > That=E2=80=99s why this RFC is supposed to be a best-compromise soluti= on between >> > strict and just casting. The hope is to appease both sides and provide >> the >> > most workable solution, really. It=E2=80=99s also the style that I, my= self, like >> > best, because it=E2=80=99s strict enough to catch bugs (I like that), = and it >> keeps >> > PHP a type-shifting language (I like that too). >> > >> > -- >> > Andrea Faulds >> > http://ajf.me/ >> > >> > >> > >> > >> > >> > -- >> > PHP Internals - PHP Runtime Development Mailing List >> > To unsubscribe, visit: http://www.php.net/unsub.php >> > >> > >> A massive +1 for this approach. I had the same thought the other day after watching this thread aganoise between "to cast, or not to cast." Trying to make some types automatically cast and some just assert (like some do already) is a road that leads to pain and suffering. Trying to make them all just assert is not "the PHP way". Trying to make them all cast (thus changing the exist array assertion into a cast action) would be bizarre and strange. So what is left? Two syntaxes, so cast and assert can both be taken care of= . foo(int $num) - for strict type compliance This HAS to be an int and an array. Nothing else will fly. bar((int) $num) - converted to type This has to be anything that could be turned into an int, which to be fair is basically anything, and anything that can be turned into an array. foo(123); // OK foo("123"); // Not OK foo("123a"); // Really not OK bar(123); // OK bar("123"); // Casts to 123 bar("123a"); // Casts to 123 bar(" 123a"); // Casts to 123 Even if you guys would like to suggest that foo("123"); should be ok because $_GET variables, I would sill strongly urge people to consider the "type assertions dont cast" approach, and add easy casting for "assert and cast" functionality. It's the most consistent and easy to explain approach, even if it does lead to the occasional developer saying "Oh, my unfiltered $_GET input is a string instead of an int. How do I... ahh I just use foo((int) $_GET['whatever']); Not an issue in 2014 I believe.