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

1

u/midir Nov 16 '14 edited Nov 16 '14

+1 parses as a valid integer, but the + is not part of the integer literal; it is a unary plus operator attached to the integer literal. Note the difference here when there is arbitrary whitespace between the + operator and number:

var_dump(+ 1); // int(1)
var_dump((int)'+ 1'); // int(0)

The difference is because whitespace is allowed in the source, but not by the string-to-int conversion, and other languages are the same in that regard.

Likewise:

var_dump(+-+-+1); // int(1)
var_dump((int)'+-+-+1'); // int(0)

var_dump(+(1)); // int(1)
var_dump((int)'+(1)'); // int(0)

My point is that merely because +1 is valid source code is not in itself a reason why it should be accepted by a general string-to-int conversion. Anther difference between source parsing and general string-to-int conversion is the treatment of leading zeros: being treated as octal in source but as decimal by string-to-int conversion.

5

u/i_make_snow_flakes Nov 16 '14

I am not sure we need to bother about the details of internal implementation here. You see, just because the implementation of unary operators ended up supporting the +/- prefixs for integer literals completely, does not mean that +/- prefixed integer literals are not a separate construct.

What I am getting at is that, '+1' and '+ 1' are separate constructs logically, even if they are supported/implemented by the same code internally. I think the manual entry reflects this fact, and is in fact correct. Just my thoughts.

1

u/midir Nov 16 '14

What I am getting at is that, '+1' and '+ 1' are separate constructs logically

Are they? The difference is one of style and convention, but they are logically and mathematically equivalent.

I was responding to your question "is the code $a = +1; valid?". And yes it is, but the source parser doesn't treat +1 specially, and so an infinite number of other complicated mathematical expressions are also valid in place of +1. But you would probably not suggest implementing a complete expression parser and eval engine in a mere string-to-int conversion function. Hence my point, that merely because +1 is valid source code is not in itself a reason why it should be accepted by a string-to-int function.

You could still make the argument that +1 as a way of writing 1 should be accepted as a matter of style or convention, but that's a different argument.

2

u/i_make_snow_flakes Nov 16 '14

When you see +1, do you read it as + applied to value 1, or as "plus one"? That is the essence of my argument...

1

u/midir Nov 16 '14

When you see +1, do you read it as + applied to value 1, or as "plus one"?

Either? It depends on the context in which I saw it.

That is the essence of my argument...

I know what your argument is. My argument is simply that that argument is not supported by merely pointing to the fact that $a = +1; compiles, since so does $a = + +1; and infinity billion other things. What is supported by a complex language's syntax is not the same as what has to be supported by a string-to-int conversion function.

Whether +1 should be supported by it regardless, is not something I'm trying to debate, since I don't care either way.

2

u/i_make_snow_flakes Nov 16 '14

My argument is simply that that argument is not supported by merely pointing to the fact that $a = +1; compiles

Not only that, but "+1" can be considered as a the representation of a single value at a higher level, and not as an expression, even though at a lower level it is treated as an expression.