Re: [RFC] Scalar Type Hints v0.2

From: Date: Wed, 14 Jan 2015 08:48:19 +0000
Subject: Re: [RFC] Scalar Type Hints v0.2
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-80446@lists.php.net to get a copy of this message
Hey Robert, > On 14 Jan 2015, at 08:22, Robert Stoll <php@tutteli.ch> wrote: > > I had a few thoughts on the new proposed declare(strict_typehints=TRUE); construct and I must > say I do not really like that we would have different behaviour just based on a directive. This is > quite ugly from a readability point of view since I as user cannot easily see whether somewhere > (above) in the code the directive was set (I have changed my mind a little bit below - left it here > for traceability purposes). Which brings me to the question what happens with the following code: > > function foo(int $a){} //is weak right? > declare(strict_typehinting=TRUE); > > > I guess it is weak since you wrote: "If this boolean value is TRUE, strict type-checking > mode is used for function calls in the remainder of the file" > Maybe it is a good idea to add such an example to the examples as well. Yes, it only affects stuff which comes after it. But in the example you just gave it doesn’t do anything, because the type checking mode is about the caller (where the function is called), not the callee (where the function is defined). A better example: <?php foo(1.0); // weak declare(strict_typehints=TRUE); foo(1); // strict > Now, having rethought my own email, I think we should enforce some code style to avoid a huge > mess. Why not declare that the directive needs to be the very first statement inside a namespace > scope (unless the directive block syntax is used). This way it would be easily verifiable for the > user if strict mode is in place or not. But then again, we could also argue that is up to code > conventions/code guidelines and they should specify whether mixing both modes is ok and where the > directive has to be placed. So re-rethinking what I just wrote I would say it is fine as it is now > but we should at least consider if we want to enforce some code practice to avoid a mess. We could possible require it to be at the top of the file, maybe. Though I expect people will probably do this anyway. I’m sure it’ll become part of PSR-whatever and there’ll be some sort of standard. By the way, you can mark some blocks as having different behaviour, e.g.: <?php declare(strict_typehints=TRUE) { foo(1); } declare(strict_typehints=FALSE) { bar(2); } This way, it’s more obvious that they have different behaviour. > Now another point: "Whether or not the function being called was declared in a file that > uses strict or weak type checking is entirely irrelevant. The type checking mode depends on the file > where the function is called." > Seems quite strange to me but fair enough, why not -- actually makes perfectly sense for this > RFC -> leaving it to the user to decide what he/she likes :-) That’s the idea. I didn’t like the idea of having an API force you to deal with a particular approach, especially since it’d mean some of your calls would be strict, others weak. This way it’s completely consistent: everything is strict, or everything is weak, and you get to choose. :) Thanks for your comments. -- Andrea Faulds http://ajf.me/

« previous php.internals (#80446) next »