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?

22 Upvotes

67 comments sorted by

View all comments

Show parent comment

5

u/[deleted] Nov 15 '14

Accepting the narrowest possible range of input means you have a much simpler rule (zero round-trip data loss), and applications can always add support for white space or plus signs or whatever themselves.

All that would be a good argument, if you were not supporting negative signs while ignoring positive signs. As it stands, you're supporting casting -10 but not +10. that's what makes it an lol, like the rest of PHP.

-4

u/[deleted] Nov 15 '14

All that would be a good argument, if you were not supporting negative signs while ignoring positive signs. As it stands, you're supporting casting -10 but not +10.

Huh, how so? That would mean you couldn't represent negative numbers.

There's no need for supporting positive signs, given numbers are implicitly positive without them.

5

u/[deleted] Nov 15 '14

numbers that start with + are also implicitly positive, but you don't support them. its inconsistent behavior to support - but not +. Mathematically, +xxx is as valid as xxx or -xxx.

-2

u/[deleted] Nov 15 '14

numbers that start with + are also implicitly positive, but you don't support them.

Er, no. That's making a number explicitly positive. Without the + it's implicitly positive.

its inconsistent behavior to support - but not +.

It's not if you think about it. + is redundant since a number without it is positive anyway. - isn't.

8

u/[deleted] Nov 15 '14

That's making a number explicitly positive.

So you agree that its explicitly a positive number, but you'll treat it as a string. Smart.

+ is redundant

Loosely typed languages are all about supporting redundencies though. PHP supports all kinds of crap, e.g 0 == false. The fact that you're not treating +100 as being a number is not only mathematically incorrect, it also breaks PHP's own shitty conventions, as well as the principle of least surprise.

-2

u/[deleted] Nov 15 '14

it also breaks PHP's own shitty conventions

Well, yes, that is rather the entire point of these functions.

1

u/[deleted] Nov 15 '14

So if someone entered a phone number, e.g +xxxxx, you'll treat that as not being an integer.

And if someone was writing a mathematical or scientific software in which people would enter +/- signs, you'll treat the positive numbers as strings.

You're not making things better, by treating mathematically accurate numbers as strings, you're actually being an even bigger dumbass than the people who did null == false.

2

u/[deleted] Nov 15 '14

So if someone entered a phone number, e.g +xxxxx, you'll treat that as not being an integer.

You really, really shouldn't be handling phone numbers as integers. For starters, the + in a phone number is significant and changes the meaning of it. Phone numbers often have infixed - or spaces. Phone numbers can have significant leading zeros. Phone numbers won't necessarily fit in a 32-bit integer. Phone numbers can't be operated on like normal numeric values. Phone numbers can contain brackets. Phone numbers can contain # and *. Phone numbers... well, you get the picture.

And if someone was writing a mathematical or scientific software in which people would enter +/- signs, you'll treat the positive numbers as strings.

Er, no, this specific function would reject the +, but you could always strip it off yourself as it's completely redundant, e.g. to_int(ltrim($value, '+')).

7

u/[deleted] Nov 15 '14

you could always strip it off yourself

So you make a programmer do extra work to make up for your idiotic decision to treat mathematically valid integers as strings.

Anyway, I'm not sure why I'm trying to convince you. I actually take great pleasure in watching the train wreck which is PHP, and seeing it get more and more convoluted is quite enjoyable. Please, do carry on, and add another 5-6 cases where it breaks existing conventions and mathematical rules. E.g, may be have 12 == 'december' return true. It would be awesome.

You're doing god's work. Keep it up.