FileGlobal Variables?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

FileGlobal Variables?

Post by Franky »

Hi, it would be nice to have variables, that are only defined Global inside one file.

Examples for use:

You have a project in which you use some code from others. For example an ImageList:

Code: Select all

FileGlobal CurWidth.l
FileGlobal CurHeight.l
FileGlobal NewList Images.l()
Procedure IL_Add(id)
     AddElement(Images())
          Images()=id
          CurWidth=ImageWidth(id)
          CurHeight=ImageHeight(id)
EndProcedure

Procedure IL_Create(width,height)
    AddElement(Images())
     Images()=CreateImage(#PB_Any,width,height)
     Curwidth=Width
     CurHeight=Height

Procedure IL_GetHeight()
      ProcedureReturn CurHeight
EndProcedure

Procedure IL_GetWidth()
      ProcedureReturn CurWidth
EndProcedure
this Code is in a special file. You could hold it as code instead of a library to be able to find bugs better or any other reason.

The Idea is, that variables defined with "FileGlobal" are known like Global variables but only in the file, they're defined in.

Why should that be usefull?
1.)To beware coders of creating bugs by unwanted changing of values, that are importend for the procedures in the Includefile.
It´s often to be seen, that programmers of Includefiles write something like "Global ID.l". But ID is , ah, let´s say not such a special name that you can be sure, that you'll never use it anywhere else.

2.)You don´t have to think about your own prefixes for variables, when creating an Includefile and you don´t have to change any variable in any IncludeFile, you use.


So, I hope, you understood anything of my "school-is-already-gone-for-2-years"-english.

Just feel free to discuss about it. :D
Give Up everything but trying!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: FileGlobal Variables?

Post by PB »

I know what you mean, Franky. I recently had to IncludeFile a source from
here but all the vars were generically-named so I had to manually go and
prefix them all with "eval_" to distinguish them from my main code. It was
a big hassle to do it as the source was big.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

In my opininon, it would be more confusing, if you've got several namespaces.
And what would you do, if you want to access a variable from an includefile?

By the way, all your procedures from your code could easily be replaced by macros... Why don't you use macros?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »

@AND1:

1.)It´s an example, I didn´t need directly this code
I just didn´t want to fill my post with a long code, which was unimportant

2.)If you want to access a variable from an includefile, just use Global instead of FileGlobal or don´t define it any way.

I don´t want global to be replaced with FileGlobal, I want FileGlobal as a feature

It´s something like protected, but not for Procedures, but for whole Includefiles.
Give Up everything but trying!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

The compiler merges all includes before compile, so it's not so easy to
implement, but i think a namespace would more usefull!

/edit:
Or like this:

Code: Select all

XIncludeFile "bla.pbi", bla_
The Compiler add bla_ as prefix to all global variables before including.

The syntax is not so important, but the feature is required :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Actually, just being able to add a prefix to all Globals and Procedures would be a good way of implementing namespaces :)
Leonhard
User
User
Posts: 55
Joined: Fri Jun 16, 2006 7:43 am

Post by Leonhard »

I found namespace's more useful then only prefix-adding for variables.

When you like to add prefixes, you can make a preparser.

But namespace's would be better and very nice.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Well, I'm still waiting for this:
http://www.purebasic.fr/english/viewtop ... ht=modules

FileGlobal and IncludeFile with prefix sound more like a workaround...
With modules lots of the problems would just disappear.
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

i second thtat, such feature is important.

Back two year in the past, i submit a solution.
http://www.purebasic.fr/english/viewtop ... highlight=

Imagine a command "IncludeLibrary"

Code: Select all

IncludeLibrary "MyWonderfullInclude.pbi"
This way, consider that no vars is included.
Only ProcedureDLL are accessible (and maybe Constants, Structures).

The scope behaviour would be similar to a DLL or a UserLib.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

@Flype
+1 :D
good way to go

greetings
Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

+1 for namespaces.
cheers,

bembulak
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Post by Franky »

Ok, I thought about it and I have to say, that IncludeLibrary is a great Idea :D .

so, +1 for that, this is better than fileglobal, I think :D
Give Up everything but trying!
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

+1 for IncludeLibrary :D
+1 for modules :D (follow the link in Remi_Meier's message above)
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Post Reply