Stas,

Thanks for the comments!

inline.

On Wed, Mar 7, 2012 at 2:31 AM, Stas Malyshev <smalys...@sugarcrm.com> wrote:
> Hi!
>
>>>> https://wiki.php.net/rfc/parameter_type_casting_hints
>
>
> Just took a look on it - the syntax proposed there is quite ugly and rather
> confusing, I really wouldn't like to have such syntax in PHP.

Ok, this is where I'm torn.  I have a few (read: about 4 or 5)
responses (mostly from people I respect quite a bit) that say it's
quite ugly and confusing.  I have had a lot more responses saying it's
simple, elegant and syntatically draws the difference between casting
and strict type checking.  I see your point, I really do.  It's not
the prettiest syntax out there.  But confusing?  I'm not so sure...  I
think you're too close to the problem to really understand it.  Or
perhaps I am...  I don't know.

But based on the conversations I've had with all sorts of devs about
this (some that I don't know personally, some that I do, some junior,
some senior), the vast majority of them got it without explanation.
The fact that cast syntax was re-used gave them enough of a hint as to
what it was doing that they guessed correctly.  Now, if you think it
should be doing something different, then definitely re-visit the
syntax.  But if it's going to be casting variables, the syntax does
make sense and carries an intrinsic value with it...

> Also "(int) $foo = “1” will generate an E_COMPILE_ERROR" makes no sense to
> me.

Actually, that makes perfect sense to me.  Think about it from the
reader perspective.  Sure, "(int) $foo = '1'" is pretty straight
forward, but what you you think if you saw:

foo ((string) $bar = 123);

Frankly, that would confuse the daylights out of me, and would force
me to stop and think about what would happen.  That's exactly what I
would like to avoid, the thinking step on reading it.  In fact, it
would make even less sense, since it would need to be cast, so the
following would return false:

function foo((string) $bar = 123) {
    return $bar === 123;
}
foo();

How much more confusing is that?  By restricting the default parameter
to be of the type being casted to, that sort of a problem wouldn't
happen and would be adverted by the compiler...

I think the difference is that I know that strings are not integers
and vise versa.  Even at the language level there are subtle
differences in behavior between the types, even if they store
equivalent values.

> Also, this line:
> function test((int) $intParam, (string) $strParam = "foo", (array) $array)
> {}
>
> is not proper PHP code - it contains optional parameter and then parameter
> with no default.

It is proper PHP code.  It parses just fine
(http://codepad.viper-7.com/rEHqZp).  Sure, it's not best practice
(and was an accident which has been fixed), but it's 100% valid and
proper PHP code and doesn't even raise E_STRICT errors...

> And can we please stop using word "hinting"?
> We can call it type conversion, typecasting, type coercion, etc.
> http://en.wikipedia.org/wiki/Type_conversion

Well, but I think that a distinction needs to be made about where it
is.  How about "Parameter Casting".  Better?

> But I don't see how there's any hinting involved.

The compiler (and any IDE) knows what the type of the variable will be
inside the function body.  How is that not hinting?

Anthony

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to