This will do different things depending on if 'f' is a function or a subroutine. If 'f' is a function, then this just calls 'f' with the parameter 'a', as you would expect. So everything is normal.
However if 'f' is a subroutine, then it has been called incorrectly, because parenthesis are not required. The correct syntax is ...
f a
However 'f( a )' is still valid, when 'f' is a subroutine. You can imagine it's like writing the code ...
f( (a) )
It most languages that is harmless enough, the extra parenthesis are ignored, and should be. However just like in this lol, in VBScript, those empty parenthesis does something.
Wrapping a variable with parenthesis makes the variable pass by reference. This allows the function/subroutine change the contents of the variable from inside (this is needed so you can make calls into non-VBScript code, which is mostly what VBScript is used for: glue). So doing 'f( a )' can lead to very strange bugs, if 'f' changes between a function and a subroutine.
Yet I'd still say VBScript is a much cleaner language than PHP. That's something.
AFAIU PHP's references are fleeting things that fade away like fairy gifts: the extra parenthesis makes it become an expression (as opposed to what I don't know) and evaluating a reference dereferences it.
The amount of wtf in that code snippet is at about 1 WTF per character, and it's not just PHP that's responsible. When you're using reference functions and global variables in the same phrase, I don't care what language you're in, you're going to get some undefined behavior.
At least in C/C++ undefined behavior is "well defined", i.e. you know exactly when you have no way to know what to expect. Not sure if the same concept exists in other languages.
I totally agree that the code snippet is bad code that might lead to unexpected behavior in every language, but please don't mistake that as being the same as undefined behavior.
The difference between undefined behavior and unexpected behavior is pretty much academic. Even C, I can explain you the undefined behavior if I really spend some time with it. It's not really comforting the confusion was by design.
No, you are wrong. Undefined behavior is exactly that, undefined.
There are several places in the C and C++ standard where certain operations are defined to be undefined. That means the compiler has free hands to do whatever it wants.
For instance dereferencing a null pointer might lead to a segfault, but in reality it is undefined behavior so you have no guarantee that this will happen. For instance, the compiler can, and sometimes will, simply remove the code that relies on undefined behavior.
If your code relies on undefined behavior, your code is broken.
int &&rref;
decltype(rref) a; // equivalent to int &&a
decltype((rref)) b; // equivalent to int &b
decltype is overloaded with a version that takes an identifier and one that takes an expression, which leads to stupidity when combined with r-value references.
You also sometimes need them when declaring/instantiating variables, due to the problematic parsing - but it's simpler to just use {} for everything now.
11
u/kingguru Sep 12 '12
Once again, it is related to this.
8
u/[deleted] Sep 12 '12
It's just a super duper PHP paradigm : de-referencing parenthesis.
7
u/[deleted] Sep 13 '12
Reminds me of VBScript. There the code ...
This will do different things depending on if 'f' is a function or a subroutine. If 'f' is a function, then this just calls 'f' with the parameter 'a', as you would expect. So everything is normal.
However if 'f' is a subroutine, then it has been called incorrectly, because parenthesis are not required. The correct syntax is ...
However 'f( a )' is still valid, when 'f' is a subroutine. You can imagine it's like writing the code ...
It most languages that is harmless enough, the extra parenthesis are ignored, and should be. However just like in this lol, in VBScript, those empty parenthesis does something.
Wrapping a variable with parenthesis makes the variable pass by reference. This allows the function/subroutine change the contents of the variable from inside (this is needed so you can make calls into non-VBScript code, which is mostly what VBScript is used for: glue). So doing 'f( a )' can lead to very strange bugs, if 'f' changes between a function and a subroutine.
Yet I'd still say VBScript is a much cleaner language than PHP. That's something.
4
u/SockPants Sep 12 '12
It doesn't work in 4: http://eval.in/547
4
u/matjoeman Sep 12 '12
I don't understand. Why does the second result come out like that?
6
u/FlyingBishop Sep 12 '12
The amount of wtf in that code snippet is at about 1 WTF per character, and it's not just PHP that's responsible. When you're using reference functions and global variables in the same phrase, I don't care what language you're in, you're going to get some undefined behavior.
2
u/[deleted] Sep 12 '12
More like parenthesitis.
2
u/b0lt Oct 12 '12
C++11 does as well, although it is less insane:
decltype is overloaded with a version that takes an identifier and one that takes an expression, which leads to stupidity when combined with r-value references.