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?

21 Upvotes

67 comments sorted by

View all comments

Show parent comment

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?

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.

-1

u/[deleted] Nov 15 '14

Simple question: is +1 a valid integer in PHP?

No. But technically -1 isn't either. Like most languages, PHP doesn't have signs in its numeric literals.

and will $a have value int(1) after this statement?

Yes, because the unary plus (+) does nothing.

5

u/i_make_snow_flakes Nov 15 '14

Please take a look here , under the heading 'Formally, the structure for integer literals is'..

it says

decimal : [1-9][0-9]* | 0 integer : [+-]?decimal

What does that mean?

0

u/[deleted] Nov 15 '14

The manual is inaccurate, that's not actually how integer literals are defined.

5

u/i_make_snow_flakes Nov 15 '14

Ha ha, this is lolphp right here.

it does not matter how it is put in the source code of php. It is the exposed behavior, and the manual says so. Anyway I don't think I want to discuss things with someone who can't respond without down voting every one of my responses. It's not that I care about downvotes, but I find the behavior moronic. So good luck with the RFC.

3

u/smog_alado Nov 15 '14

She is right about integer literals not having a negative sign in them. PHP (and most other languages) parse -10 as an unary-minus operator followed by an integer literal instead of parsing it directly as a negative number.

That said, it seems really weird to make an exception in the conversion function allowing for "-" but not do the same for "+".

1

u/[deleted] Nov 15 '14 edited Nov 15 '14

it does not matter how it is put in the source code of php. It is the exposed behavior, and the manual says so.

No, the manual is flat-out wrong. PHP doesn't have signs on its literals. You can't actually enter the minimum integer value in source code for this reason. Go ahead, try it!

$ php -r 'var_dump(-9223372036854775808);'
float(-9.2233720368548E+18)

Also see the language specification: https://github.com/php/php-langspec/blob/master/spec/09-lexical-structure.md#integer-literals

The manual is wrong.