Page 1 of 1

Blocks of Code (Block/EndBlock ?)

Posted: Wed Aug 29, 2012 1:07 pm
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Wed Aug 29, 2012 2:36 pm
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Wed Aug 29, 2012 3:37 pm
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Wed Aug 29, 2012 4:07 pm
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:

Re: Blocks of Code (Block/EndBlock ?)

Posted: Wed Aug 29, 2012 4:32 pm
by Shield
I think those seven years make it quite clear what the priority of this feature is.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Thu Aug 30, 2012 7:11 am
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)

Re: Blocks of Code (Block/EndBlock ?)

Posted: Thu Aug 30, 2012 9:38 am
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Thu Aug 30, 2012 1:27 pm
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Thu Aug 30, 2012 1:43 pm
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.

Re: Blocks of Code (Block/EndBlock ?)

Posted: Thu Aug 30, 2012 11:57 pm
by MachineCode
grabiller wrote:you have to open your mind
If you open your mind, your brain will fall out. ;)

Re: Blocks of Code (Block/EndBlock ?)

Posted: Fri Aug 31, 2012 5:00 pm
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:

Re: Blocks of Code (Block/EndBlock ?)

Posted: Fri Aug 31, 2012 10:08 pm
by Joakim Christiansen
I suggested this but people didn't understand:
http://www.purebasic.fr/english/viewtop ... =3&t=45659

Re: Blocks of Code (Block/EndBlock ?)

Posted: Sat Sep 01, 2012 12:05 am
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.