Page 1 of 1

FileGlobal Variables?

Posted: Sun Jun 22, 2008 12:46 am
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

Re: FileGlobal Variables?

Posted: Sun Jun 22, 2008 1:48 am
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.

Posted: Sun Jun 22, 2008 11:34 am
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?

Posted: Sun Jun 22, 2008 12:13 pm
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.

Posted: Sun Jun 22, 2008 12:51 pm
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:

Posted: Sun Jun 22, 2008 2:19 pm
by Foz
Actually, just being able to add a prefix to all Globals and Procedures would be a good way of implementing namespaces :)

Posted: Sun Jun 22, 2008 8:52 pm
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.

Posted: Mon Jun 23, 2008 6:34 pm
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.

Posted: Mon Jun 30, 2008 9:52 pm
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.

Posted: Mon Jun 30, 2008 10:14 pm
by ts-soft
@Flype
+1 :D
good way to go

greetings
Thomas

Posted: Tue Jul 01, 2008 3:28 pm
by bembulak
+1 for namespaces.

Posted: Thu Jul 03, 2008 9:24 am
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

Posted: Sat Jul 05, 2008 5:01 am
by Blue
+1 for IncludeLibrary :D
+1 for modules :D (follow the link in Remi_Meier's message above)