Page 1 of 4

Modules

Posted: Mon Aug 08, 2005 1:16 pm
by GedB
When programming projects get to a certain size PureBasic starts to struggle.

The biggest problem is all the global stuff. Especially Arrays and Linked Lists.

It would be great if there was modular support.

At the moment we have a Global Space. Inside that global space exist all of the procedures, arrays, linked lists, global variables and object cursors.

By object cursors I mean the current object being used for images, files, databases, libraries, etc.

It would be great if the global space could be replaced with a module space. All of these global objects would then be contained within a module space.

There would be a default module, which everything would be contained in by default. This would mean that existing code would work without alteration inside the default module.

If a user calls 'UseModule(module_name) then control would switch to that module. UseModule() without a name would return control to the default modele.

For example, the following code would debug 7 followed by 5:

Code: Select all

global X
x = 5

UseModule(My_Module)
x = 7

debug x

UseModule()

debug x
I'm sure the benefits of this would be obvious to anybody who has tried to write a library.

A command should exist for renaming modules, with settings available in the IDE. This will help to work around any name clashes that might occur.

So, for example, if both Joe and Bob have a My_HashTable Module and you want to use both the code can do this:

Code: Select all

IncludeFile(Bobs.pb)
IncludeFile(UsesBobs.pb)
IncludeFile(AlsoUsesBobs.pb)
RenameModule(My_HashTable, Bobs_HashTable) 
;  Everything up to this that referred to the My_HashTable is now mapped to Bobs_HashTable instead.

IncludeFile(Joes.pb)
IncludeFile(UsesJoes.pb)
IncludeFile(AlsoUsesJoes.pb)
RenameModule(My_HashTable, Bobs_HashTable) 
;  Everything up to this that referred to the My_HashTable is now mapped to Joes_HashTable instead.

UseModule(Bobs_HashTable)
; Work with Bobs library


UseModule(Joes_HashTable)
; Work with Joes library

Back to my library.
It should also be possible to Include and exclude modules into the default space.

For example IncludeModule(Joes_HashTable) would merge both the current module and the default space. ExcludeModule(Joes_HashTable) would then remove it. Then you could do this:

An optional 'Prefix' paramater would also be useful so that if Joes_Hashtable included the linked lists AllHashes() then a call to IncludeModule(Joes_HashTable, "Joes_" would make the procedure Joes_AllHashes() available in the default space.

Taking it further, module spaces could be created for older versions of the purelibraries. For example, all the hassles brought with the memory changes to memory could have been avoided. UseModule(Default_3.81) would return behaviour for AllocateMemory and the rest to the pre-3.90 style. Old code could be wrapped just have to be wrapped in these to ensure legacy support.

Even better, their could be module like 'Removed_In_3.90' and 'Introduced_In_3.90'. The latter would be automatically included in the default namespace.

The the following code would effectively roll back the compiler to before the change:

Code: Select all

ExcludeModule(Introduced_In_3.90)
IncludeModule(Removed_In_3.90)
There could be an agreemeent that such transitional modules will remain for 12 months and then be removed. This would give those with a large body of purebasic code to support plenty of time to adapt, and these features would give them so many options on how best to handle the transition.

Posted: Mon Aug 08, 2005 5:41 pm
by Dare2
It took me a while to read through that and (partially) understand the advantages.

But this sounds pretty good! I wonder how difficult it would be to implement?

Posted: Wed Aug 10, 2005 11:50 pm
by Rescator
You can do the same now, using structures!

Just use a index (array or list) of banks (structures) that contain all the data, pointers, etc.

This way you could simply specify a different index id, and you could read or write in a different bank (or memory bank as I guess you could also call it).

Posted: Thu Aug 11, 2005 1:50 am
by dracflamloc
I really think that there is a major need for non-global arrays.... I wish fred would implement this sometime soon.

(I know you could make a structure with an array inside it, and then declare multiple of those structures)

Posted: Thu Aug 11, 2005 8:25 am
by dmoc
When programming projects get to a certain size PureBasic starts to struggle.
Agree 100% - moaned about this some time ago. I have only recently been considering returning to a large-ish PB project simply because of the improved debugger (well done Fred and crew!). Still that leaves me with the problem of how to restructure my code to make it more manageable and modules would be great for this. It would also be absolutely fandozy (techy term) if modules supported seperate string spaces so we could multi-thread without problems. Bring it on!

Posted: Thu Aug 11, 2005 12:14 pm
by Fred
This module concept sound good for bigger projects, and we will think how to implement it cleanly.

Posted: Thu Aug 11, 2005 1:28 pm
by GedB
Thanks Fred.

I'm sure you'll be able to come up with something much cleaner than my efforts :)

Posted: Thu Aug 11, 2005 4:03 pm
by remi_meier
Just take a look at OBERON. It's a programming language based on
modules! I think Niklaus Wirth implemented it in a very good way!
(this way, it would also include a kind of OOP)

I think it was this site (http://oberon.ethz.ch/), but it doesn't load for
me at the moment...

But GedB's idea would also be enough for me.

greetz
Remi

Posted: Thu Aug 11, 2005 4:33 pm
by dmoc
Successor to Modula. Both a language and an OS. Pretty good in fact. For those who like niche OS's

Posted: Thu Aug 11, 2005 9:08 pm
by GedB
I was taught modula 5 at Uni.

Posted: Fri Feb 09, 2007 5:00 pm
by remi_meier
Is there a chance for us to see this within this year?

It would be really cool if we could
- create modules
- define which procedures should be public (cleanly define the interface)
- perhaps with public variables, too
- separate compilation of each module (probably to .OBJ files)
- include them like normal include-files and optionally also define, which
procedures shall be included (smaller executable)

My syntax suggestion:

Code: Select all

Module "one"
Export
  MyFunc.s(a.l, b.l)
  myvar.q
EndExport

Global myvar.q = 7

Procedure.s myintern()
  ProcedureReturn "yeah!"
EndProcedure

Procedure.s MyFunc(a.l, b.l)
  ProcedureReturn myintern()
EndProcedure

EndModule
One could also use the ProcedureDLL approach, but it's not a DLL :D and
a cleanly defined interface separated from the code is much more readable
and IMO cleaner.
And include it like

Code: Select all

IncludeModule "one"  ; would import everything declared in the module as public
and optionally

Code: Select all

IncludeModule "one"
  MyFunc.s(a.l, b.l) ; don't include myvar.q
EndIncludeModule

A module could also be compiled to a .OBJ file for use in other languages.

IMO it would be the best feature ever implemented since ~3 years :wink:

Posted: Sun Jun 24, 2007 8:00 am
by Amundo
Fred said:

This module concept sound good for bigger projects, and we will think how to implement it cleanly
Hi Fred,

Any more on this, please?

Posted: Mon Jul 02, 2007 3:54 am
by Rescator
Might be better to merge this module idea with project libs instead.
PB compiling code or a set of includes into PB libs of some sort.

AmigaE had modules like this, not sure about how memory and stuff like that was used though, but the semi compiled (can't recall exactly what they where, I hope Fred might recall though)
modules was very practical as they basically contained the code needed to call the code within that module. (again my memory is hazy whether a module included or just linked to other modules, I think it included though so a module that included 3 other modules would be bigger in size than mere linking would, this would also reduce issues of code changes in other modules, provided the referenced module is not recompiled)

Posted: Sat Jul 05, 2008 4:50 am
by Blue
Fred wrote:This module concept sound good for bigger projects, and we will think how to implement it cleanly.
PB in the tracks of Modula and ADA ? Wow. That will be great indeed.
Can we have it tomorrow? (just joking... Sunday will be fine.)
GedB wrote:Thanks Fred.
I'm sure you'll be able to come up with something much cleaner than my efforts :)
Don't sell yourself short. You've presented a very clear case for a great improvement to our already excellent PB.

May I squeak a one penny suggestion along GedB's suggestions ?
Falling back into the default module should be a very clear explicit command.
GedB suggested UseModule();
I would say UseMain or UseDefault.
Same difference (as they say here), but clearer.

Posted: Sat Jul 05, 2008 9:11 am
by blueznl
Module sounds pretty much to me like workspaces... or am I missing the point here?