Scope/EndScope keywords

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Scope/EndScope keywords

Post by UserOfPure »

I'd like to see Scope and EndScope keywords added -- should be easy! What they do is: the Scope one takes a single text parameter, and that text is (internally) added to the start of all variable names, procedure names, etc, from that point, until EndScope is reached.

What this means, is that you can copy and paste a snippet from these forums into your own app, wrap them with Scope/EndScope, and you are guaranteed that the variable names, procedure names, etc, from the snippet will NOT clash with your existing code at all (assuming you use a unique Scope name).
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@UserOfPure:

How would global [and shared] variables be treated?

I doubt whether it would always be appropriate to prefix their names with the prefixed scope text.
Anthony Jordan
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

to avoid clash with own names, it's strongly recommended to use Protected inside of Procedures,
especially when you want to write portable includes.

additionally, just copying code from the forums into your own project without adjusting it is no deed of glory.
oh... and have a nice day.
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Post by UserOfPure »

akj wrote:How would global [and shared] variables be treated?
In the same way. A global var named Joey in the snippet, with a scope parameter of Big, would just become a global var named BigJoey instead, meaning that it wouldn't clash with a main app global var named Joey. The snippet and main app would compile together without problem.
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

Kaeru Gaman wrote:[...]copying code from the forums into your own project without adjusting it is no deed of glory.
Couldn't have been said any better. And that's the crux of the matter.

For the life of me, i just can't think of a single situation when you'd want to do that.
If you want to test a snippet, you copy it into a new code window.
If you want to integrate the snippet into your code, you adjust and adapt it.

But to compile a snippet, as is, right into your existing code ? My good grief...
Why ?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Post by UserOfPure »

Kaeru Gaman wrote:it's strongly recommended to use Protected inside of Procedures
You can't use "Protected" with procedure names.
Blue wrote:to compile a snippet, as is, right into your existing code ? My good grief.
There are plenty of good standalone snippets in these forums by respected coders who write clean, efficient code for us to use. These such examples need no adaptions, and for you to say "good grief" about it means that you're basically trashing those good contributions by top coders here. I'm sure they appreciate being told that their efforts in the "Tricks N Tips" section are nothing more than crap.

I see no problem with allowing a drag-and-drop of such code into our apps. With Scope/EndScope, it just means we don't need to worry about adapting their code, or our code: just drop it in, and it's ready to run. No need to waste time picking the code apart and checking for clashing variable names and procedure names.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

UserOfPure wrote:You can't use "Protected" with procedure names.

There are plenty of good standalone snippets in these forums by respected coders who write clean, efficient code for us to use. These such examples need no adaptions, and for you to say "good grief" about it means that you're basically trashing those good contributions by top coders here. I'm sure they appreciate being told that their efforts in the "Tricks N Tips" section are nothing more than crap.

I see no problem with allowing a drag-and-drop of such code into our apps. With Scope/EndScope, it just means we don't need to worry about adapting their code, or our code: just drop it in, and it's ready to run. No need to waste time picking the code apart and checking for clashing variable names and procedure names.
/me shakes head

if you want to include codesnippets into your project, put it into an include and do a Find/Replace with
"Procedure " -> "Procedure Prefix_"

even with scope/endscope there is no guarantee that dragged-dropped code will work flawlessly.
you will have to check it over anyways.
if you want to code, put work into it.
if you don't want to put work into it, buy a click-together-kit and drop programming.

we did not say, you should not appreciate code from the forums, we just said
if you copy/paste it without investing the minimal work to put prefixes into it you do not appreciate it at all! :evil:
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Search and replace is the digital manifestion of the devil.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

:roll: ei die katz die graue du ahnst es nicht...
oh... and have a nice day.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

There is indeed much wonderful code in the forums.

But, as Mr Gaman mentioned, dropping it into your program and hoping it works as you expect it to, is potentially setting yourself up for problems down the road.

cheers
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

... which all has nothing to do with the request :-)

+1

Although this sounds amazingly similar to the concept (and associated discussion) on modules and workspaces :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Forgetting code posted in the forums for a moment, it would be useful to have a feature similar to this to make it easier to reuse my own code in other programs.

Prefixing every global variable and procedure and structure works but it has the failing that you can't improve a portion of code that is used in several different programs without the need to rename everything before it can be re-included in different codes. Choosing a great prefix from the start works well for code that is then included in different projects so that re-prefix everything is unnecessary, but it has the failing that it can make code much harder to read.

The reuse of code increases as the size of a project increases. If prefixing the names of variables, procedures, and structures is the common solution than why not turn all that repetitive action into a simple solution with a new keyword or two.

I am anxious for this to be implemented in some way, and Fred has said that it was something he would consider (look in the second link below from 2005). I am waiting for that item to advance on his ToDo list. :D


Note: this topic is simply a variation of namespaces and modules discussed in other threads and feature requests. Here are some links to those discussions.

http://www.purebasic.fr/english/viewtopic.php?t=36485
http://www.purebasic.fr/english/viewtopic.php?t=16224
http://www.purebasic.fr/english/viewtopic.php?t=32954
http://www.purebasic.fr/english/viewtopic.php?t=30163
http://www.purebasic.fr/english/viewtopic.php?t=25478
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

This is all nice and everything but what point is there to scope/endscope without being able to pre-define a scope name? Like packages in Java and namespaces in c++?

Without being able to import a named scope this feature would very little use.
Post Reply