Hazard warning: hidden macro

Share your advanced PureBasic knowledge/code with the community.
Grunz
User
User
Posts: 59
Joined: Sat Nov 12, 2011 7:21 pm

Hazard warning: hidden macro

Post by Grunz »

It might have been mentioned somewhere already, but I did not know what search term to use...

x + 5 notation considered harmful!


The term

Code: Select all

x+5
is seemingly the same as

Code: Select all

x += 5
in C.
But actually it is a hidden macro and the compiler will expand it to x = x + 5.

The importance of the difference can be seen is this example:

Code: Select all

some_array( Random(42) ) + 1
If it were the same as C's += then It'd increase the array at a random position by 1.
But it will expand to

Code: Select all

some_array( Random(42) ) = some_array( Random(42) ) + 1 
.
Which will instead add the content of a randomly determined other slot with 1 and assign that.

So, in the first case, it is not a relevant shortening of the code to write x + 5 instead of x = x + 5.
If you have some complex expression, then it might be shorter, but just then it is dangerous to use.
Overall conclusion is to avoid it just like goto.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Hazard warning: hidden macro

Post by IdeasVacuum »

.... good or not good, I never felt comfortable with the syntax, but I have used it in forum snippets 'to conform'.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hazard warning: hidden macro

Post by MachineCode »

Grunz wrote:Which will instead add the content of a randomly determined other slot with 1 and assign that.
So? I see no error with that.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Hazard warning: hidden macro

Post by netmaestro »

There is an error with that, the macro expansion is causing the Random() command to be executed a second time and the first slot, rather than being incremented, instead gets the incremented value of another randomly selected slot assigned to it. Very much not the expected result. At any rate, this gets mentioned from time to time and it's well to remember that you can't use any call, command or function with this code shortcut because it will get called twice.
BERESHEIT
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Hazard warning: hidden macro

Post by Joakim Christiansen »

Grunz wrote:The importance of the difference can be seen is this example:

Code: Select all

some_array( Random(42) ) + 1
If it were the same as C's += then It'd increase the array at a random position by 1.
But it will expand to

Code: Select all

some_array( Random(42) ) = some_array( Random(42) ) + 1 
.
Which will instead add the content of a randomly determined other slot with 1 and assign that.
This is basically code that doesn't function as intended... Therefore I would categorize this as a bug(!!) in PureBasic since the compiler should be intelligent enough to avoid such stupidity.
I like logic, hence I dislike humans but love computers.
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Hazard warning: hidden macro

Post by freak »

Its all just a matter of definition.

x + 1 is just a shorthand for x = x + 1. Therefore,

Code: Select all

some_array( Random(42) ) + 1
is just a shorthand for

Code: Select all

some_array( Random(42) ) = some_array( Random(42) ) + 1 
The compiler generates the correct output for that statement. So where is the bug?

The behavior is logical and consistent. Just because you like it to behave otherwise does not make it a bug.
quidquid Latine dictum sit altum videtur
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Hazard warning: hidden macro

Post by luis »

freak wrote:Its all just a matter of definition.
That's true. Is this defined using the terms you just used in the help file ?

I found this:
HELP FILE wrote: If the result of this operator is not used and there is a variable on the LHS, then the value of the expression on the RHS will be added directly to the variable on the LHS.
and about the "programming by examples" concept:
HELP FILE wrote: variable+expression ; The value of "expression" will be added directly to the variable "variable"

So

some_array( Random(42) ) + 1

should mean

1 ; the value of the expression

is added to

some_array( Random(42) ) ; the variable

The above is one (ONE) precise variable element inside the array

Looks like a bug to me.

If it was intended to work this way should be documented but looking the above excerpts from the manual give me the impression this was not the intention...
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Hazard warning: hidden macro

Post by Joakim Christiansen »

luis wrote:If it was intended to work this way should be documented but looking the above excerpts from the manual give me the impression this was not the intention...
+1
I like logic, hence I dislike humans but love computers.
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Hazard warning: hidden macro

Post by Tenaja »

+1

When the manual says this:
variable+expression ; The value of "expression" will be added directly to the variable "variable"
...and NOT this...
variable+expression ; This is expanded out to "variable" = "variable" "expression"
It is is a confirmed bug, because by the definition given in the manual, the gode generation fails. HOWEVER, the bug can be squashed by a simple manual update, if the latter was the original intent.

I really hope the code is changed, though, because it will be much more convenient to have it behave "as it should." Not only did we "all" write code based on the current manual text, it is also more "convertable" to other languages to keep the existing manual text.

BTW, the manual is misleading in any case, if one uses the multiplication operator...
x*4 ... the expression (4) is added to the variable...
It should read "The expression is carried out upon the variable." With this text, it is accurate regardless of which operator is used (add, subtract, multiply, etc)
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Hazard warning: hidden macro

Post by luis »

Tenaja wrote: BTW, the manual is misleading in any case, if one uses the multiplication operator...
x*4 ... the expression (4) is added to the variable...
That was specifically for the "+". The sentence is repeated and adjusted for each operator:
HELP FILE wrote: variable*expression ; "variable" will be multiplied directly by the value of "expression"
Tenaja wrote: HOWEVER, the bug can be squashed by a simple manual update, if the latter was the original intent.
Yes, even if I would bet it was not. But only Fred can comment on that.
"Have you tried turning it off and on again ?"
A little PureBasic review
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Hazard warning: hidden macro

Post by freak »

The original intend was to have a simple shorter way to update a variable. It was never meant to be used with complex expressions (not to mention function calls with side effects such as Random()) on the left hand side.
quidquid Latine dictum sit altum videtur
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Hazard warning: hidden macro

Post by luis »

freak wrote:The original intend was to have a simple shorter way to update a variable. It was never meant to be used with complex expressions (not to mention function calls with side effects such as Random()) on the left hand side.
OK, thanks. So the help should read:

variable+expression ; The value of "expression" will be added directly to the variable "variable". Well, not really. Unless "variable" is actually an expression too complex or causing side effects since the shorthand is expanded to variable=variable+expression and I'm not keeping track of the original destination variable. So a variable expression resulting in different outcomes for every instance of it could bring all the negative consequences of the case. Do you follow me ? What did you expect ?"

I'm joking, but this is what happen when you have to guess expecting something different because commonly accepted elsewhere (for a reason IMHO) and not clearly stated differently in the manual.

I just made a post requesting to mention this in the "PureBasic Docs" thread -> http://www.purebasic.fr/english/viewtop ... 95#p390495

:wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Hazard warning: hidden macro

Post by freak »

Very funny.

The help just needs to read: Statements of the form <exprA> + <exprB> are expanded to <exprA> = <exprA> + <exprB>. This is not complicated and clearly states the current behavior. I really don't see why this is such a big deal.

Btw, if you want this behavior to be different, feel free to post it as a feature request. Screaming bug or whipping out the sarcasm won't get you anywhere.
quidquid Latine dictum sit altum videtur
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Hazard warning: hidden macro

Post by luis »

The help just needs to read: Statements of the form <exprA> + <exprB> are expanded to <exprA> = <exprA> + <exprB>. This is not complicated and clearly states the current behavior. I really don't see why this is such a big deal.
Maybe it's not big. But it's a problem for someone or this thread would not exist. Hence the request in the appropriate thread after your clarification.
Btw, if you want this behavior to be different, feel free to post it as a feature request. Screaming bug or whipping out the sarcasm won't get you anywhere.
Thanks for the tip.
I didn't scream. I considered the options (and historically weighted the possibilities of a modification of the current behavior for something of this kind) and decided a little sarcasm was the most logical choice. I don't see you threading lightly in the forums, BTW. I was a feather.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Hazard warning: hidden macro

Post by Psychophanta »

freak wrote:Its all just a matter of definition.

x + 1 is just a shorthand for x = x + 1. Therefore,

Code: Select all

some_array( Random(42) ) + 1
is just a shorthand for

Code: Select all

some_array( Random(42) ) = some_array( Random(42) ) + 1 
The compiler generates the correct output for that statement. So where is the bug?

The behavior is logical and consistent. Just because you like it to behave otherwise does not make it a bug.
(1Up)
Indeed, all depends on what do you want to do
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply