Protected Variables w/in a block.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Protected Variables w/in a block.

Post by jassing »

Given the example below, I would have liked to see the line [Debug "2: "+cFish] error out; this would be inline with things like c & c++

Code: Select all

Procedure m()
	If 1
		Protected cFish.s = "HI"
		If 1
			Debug "1: "+cFish
		EndIf
	EndIf
	Debug "2: "+cFish
EndProcedure

m()
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Protected Variables w/in a block.

Post by skywalk »

Huh :?:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Protected Variables w/in a block.

Post by BorisTheOld »

I think he wants "cFish" to be local to the If statement.

Seems to me that it's just one more way to write stupid code, since it implies that the following would be valid:

Code: Select all

Procedure m()
   Protected cFish.s = "World"
   If 1
      Protected cFish.s = "Hello"
      If 1
         Debug "1: "+cFish
      EndIf
   EndIf
   Debug "2: "+cFish
EndProcedure

m()
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Protected Variables w/in a block.

Post by jassing »

I guess you never used C... that's very common; in that a block of code can have it's own variables, or share with parent blocks...

No need to shit on me because you don't agree.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Protected Variables w/in a block.

Post by Shield »

I agree with jassing, I'd like to see that.

The code shown by BorisTheOld will of course be invalid since variables
are passed down the scopes and cannot be redefined.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Protected Variables w/in a block.

Post by skywalk »

I was just thrown by the example with 'If 1' always true.
If you change to 'If 0' and use EnableExplicit then I am curious why no error?
Maybe look to the ASM to see that all procedure variables are declared regardless of condition?

Code: Select all

EnableExplicit
Procedure m()
  If 0
    Protected cFish.s = "HI"
    If 1
      Debug "1: "+cFish
    EndIf
  EndIf
  Debug "2: "+cFish  ;<-- Why no error for undefined variable cFish?
EndProcedure
m()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Protected Variables w/in a block.

Post by freak »

Shield wrote:The code shown by BorisTheOld will of course be invalid since variables
are passed down the scopes and cannot be redefined.
In PB they can be redefined though. Also, without EnableExplicit a variable is declared on its first use. Both of these rules do not fit well with this kind of block scoping.
quidquid Latine dictum sit altum videtur
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Protected Variables w/in a block.

Post by BorisTheOld »

Yes, that was my point - we're talking about PB and BASIC.

I was expressing the opinion that it's a bad idea to allow local scope within If/EndIf blocks and similar constructs. I've seen this type of usage in other languages and the aftermath is not something one would wish to debug.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Protected Variables w/in a block.

Post by Shield »

@freak

Well they can be redefined now, that behavior obviously has to go if this feature gets implemented.
I didn't realize they can be "defined" again, because I never did this (why should I?). IMHO, this shouldn't
even be possible and since types can't be changed, I guess the keyword just gets ignored the second
time it is used.

The second part seems to be a valid point though (however, I always use EnableExplicit).
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Protected Variables w/in a block.

Post by skywalk »

What can be redefined?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Protected Variables w/in a block.

Post by Shield »

You can use Protected / Define / Global multiple times on the same variable without getting an error message.
Most other languages I know will throw an error if there is an attempt to redefine a variable.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Protected Variables w/in a block.

Post by skywalk »

PB gives an error too :wink:

Code: Select all

EnableExplicit
Procedure m()
  If 0
    Protected cFish.s = "HI"
    If 1
      Debug "1: "+cFish
    EndIf
  Else
    ;Protected cFish.s = "NO"  ;<-- Local variable already declared
  EndIf
  Debug "2: "+cFish  ;<-- Why no error for undefined variable cFish?
EndProcedure
m()
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Protected Variables w/in a block.

Post by Shield »

For some reason I didn't check protected, but it doesn't throw an error with Define / Global.
Arguably it does make sense to allow that for Shared / Global usage, even though that's
very likely bad programming practice in most cases.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Protected Variables w/in a block.

Post by langinagel »

Just a question:

what is the advantage to use the very same name of a variable in a certain block?

From my point of view it decreases readability and the proper understanding of the code.
Security of knowledge by obscurity?

By the way: why not extracting the block into a procedure?

See: Robert Martins "Clean Code".

Greetings
LN
https://www.doerpsoft.org

Boost. Work. Efficiency.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Protected Variables w/in a block.

Post by Tenaja »

IMHO, it there is no benefit. However, perhaps someone likes using variable named (such as i) for every single For-loop that has no significance in the var name... that would be the only reason (as bad as it may be) that I can think of.
Post Reply