r/lolphp • • Nov 15 '14

new safe casting function RFC. casting "-10" to string is valid but casting "+10" is not..

here the comment where one user asked the author of RFC about this. I am not able to follow his reasoning. What do you think?

23 Upvotes

67 comments sorted by

View all comments

Show parent comment

-7

u/[deleted] Nov 15 '14

limited or tailored to filtering input values

What?

6

u/i_make_snow_flakes Nov 15 '14

Just that I think that is not a valid use-case for a casting function...

-5

u/[deleted] Nov 15 '14

It's not filtering, it's casting with a measure of validation. This is needed because PHP's current explicit casts ((int) etc.) never fail, which is quite dangerous and you can mangle input with them.

6

u/i_make_snow_flakes Nov 15 '14

it's casting with a measure of validation

There. This is what is wrong. It is actually two things but the name says only one, which is casting. And you end up having to implement this weird behavior with respect to the first half (which the name implies), to support the other half of the functionality.

And you seem to be down voting me before replying. really?

-2

u/[deleted] Nov 15 '14

There. This is what is wrong. It is actually two things but the name says only one, which is casting.

Uh, many languages have casting functions which can fail.

4

u/i_make_snow_flakes Nov 15 '14

They fail when the value cannot be casted. Not when it does not fit the expected format...

-1

u/[deleted] Nov 15 '14

That's the same thing.

3

u/i_make_snow_flakes Nov 15 '14

No, it's not.

-2

u/[deleted] Nov 15 '14

OK, elaborate on what the difference is, then. Fundamentally, no value can't be casted somehow. If something "can't be casted", that means they've chosen not to support a specific type or format of input, or the reverse, chosen only to support a specific type or format.

4

u/i_make_snow_flakes Nov 15 '14

Simple question: is +1 a valid integer in PHP? is the code $a = +1; valid? and will $a have value int(1) after this statement?

And are you really down voting me before replying?

→ More replies