Modules

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Modules

Post 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.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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?
@}--`--,-- A rose by any other name ..
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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).
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post 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)
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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!
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This module concept sound good for bigger projects, and we will think how to implement it cleanly.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Thanks Fred.

I'm sure you'll be able to come up with something much cleaner than my efforts :)
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post 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
Athlon64 3700+, 1024MB Ram, Radeon X1600
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Successor to Modula. Both a language and an OS. Pretty good in fact. For those who like niche OS's
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

I was taught modula 5 at Uni.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post 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:
Athlon64 3700+, 1024MB Ram, Radeon X1600
Amundo
Enthusiast
Enthusiast
Posts: 200
Joined: Thu Feb 16, 2006 1:41 am
Location: New Zealand

Post 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?
Win10, PB6.x, okayish CPU, onboard video card, fuzzy monitor (or is that my eyesight?)
"When the facts change, I change my mind" - John Maynard Keynes
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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)
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Module sounds pretty much to me like workspaces... or am I missing the point here?
( 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... )
Post Reply