Blocks of Code (Block/EndBlock ?)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Blocks of Code (Block/EndBlock ?)

Post by grabiller »

Hi,

To make it simple: having code blocks like {..} in C, allowing Protected variables.

Usefull with macros for instance to be used in several place inside the same procedure but needing to use some 'local' variables.

For instance:

Code: Select all

Macro CHECK_ERR( e_ )
  Block
    Protected err_ = e_
    Select err_
      Case 0
        ; OK
      Case 1
        ConsoleError( "Error ["+ Str(err_) +"] in "+ #PB_Compiler_Procedure )
      ; etc..
    EndSelect
  EndBlock
EndMacro

:Then:

Procedure SomeProcedure()
  ; some code..
  CHECK_ERR( MyProcedureA() )
  ; some more code..
  CHECK_ERR( MyProcedureB() )
  ; etc..
EndProcedure
Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Blocks of Code (Block/EndBlock ?)

Post by gnasen »

I ran into this problem, too. Maybe it would be enough to set an compiler option, that variables can be declared multiple times in a procedure etc.
pb 5.11
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by grabiller »

gnasen wrote:I ran into this problem, too. Maybe it would be enough to set an compiler option, that variables can be declared multiple times in a procedure etc.
Indeed that would be the simplest solution.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Blocks of Code (Block/EndBlock ?)

Post by PMV »

Modules: http://www.purebasic.fr/english/viewtop ... =3&t=16224
Thats what we could get in future if fred doesn't have forgotten. :wink:
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by Shield »

I think those seven years make it quite clear what the priority of this feature is.
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
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Blocks of Code (Block/EndBlock ?)

Post by BorisTheOld »

Why not just add an extra parameter to the macro? This parameter could then be used to create unique names for each use of the macro.

We use a 3 digit number that gets appended to other values within the macro to create unique names:

MacroName(parm1, parm2, 100)
MacroName(parm1, parm2, 101)
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
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by grabiller »

BorisTheOld wrote:Why not just add an extra parameter to the macro? This parameter could then be used to create unique names for each use of the macro.

We use a 3 digit number that gets appended to other values within the macro to create unique names:

MacroName(parm1, parm2, 100)
MacroName(parm1, parm2, 101)
That's a workaround, yes, but conceptualy it's like calling a Procedure with the names of its local variables as arguments each time you call it.

The idea is more that you don't have to think about those 'local variables' when you use these macros.

But that's a workaround, yes.

In my case I use another workaround, in each Procedure where I use such 'CHECK_ERR' macros I start the Procedure with a 'CHECK_ERR_INIT' macro wich is simply:

Code: Select all

Macro CHECK_ERR_INIT
  Protected err_.b
EndMacro
Then I directly use the err_ variables in the other macros.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Blocks of Code (Block/EndBlock ?)

Post by BorisTheOld »

grabiller wrote: That's a workaround..........
I wouldn't call it a workaround - I'm just using macros to do what I want them to do.

Ideally, I should be able to write my programs with just two statements and leave the compiler to do all the work for me.

Code: Select all

Data
Code
And although it would be nice to have, I can't see Fred adding that as a feature. :)

Sometimes a "nice to have" feature is just extra "fluff" that doesn't add anything significant to the language.
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
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by grabiller »

BorisTheOld wrote:../..I'm just using macros to do what I want them to do../..
That's exactly my point.

Macro in PureBasic are great, but they just lack this ability to undefine a Macro.

This may be not a big deal, but at the same time I doubt it's something very complicated to implement. This is just the kind of 'little' features that makes your life easier.

With this feature added, PureBasic Macro would be as powerfull as C/C++ ones.

Even better because we allready have the convenience to not loose the syntax coloring and have to type '\' at the end of each line ; -)

Regarding extra "fluff" you have to open your mind and exit your tunnel vision :
A car air-bag does not add anything significant to the car itself.. but it can be a life saver.. for you ;-)

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Blocks of Code (Block/EndBlock ?)

Post by MachineCode »

grabiller wrote:you have to open your mind
If you open your mind, your brain will fall out. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Blocks of Code (Block/EndBlock ?)

Post by BorisTheOld »

grabiller wrote:......you have to open your mind and exit your tunnel vision :
I have an Eidetic memory and Spatial Sequence Synesthesia -- would that be sufficient? :lol:
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
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by Joakim Christiansen »

I suggested this but people didn't understand:
http://www.purebasic.fr/english/viewtop ... =3&t=45659
I like logic, hence I dislike humans but love computers.
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Blocks of Code (Block/EndBlock ?)

Post by grabiller »

Joakim Christiansen wrote:I suggested this but people didn't understand:
http://www.purebasic.fr/english/viewtop ... =3&t=45659
Well we are 2 now, perhaps we could start an online community for local scoping in PureBasic :wink:

But for sure, at least one another person understand this issue: Fred.

Local scoping (like macro redefinition) is a small thing to implement (compare to some OO requests or other exotic features) but gives a lot, and it always puzzles me to see so much reluctance for such powerfull concepts.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
Post Reply