Allow a ImportModule ... As ..., or a RenameModule old, new
Posted: Sat Jun 29, 2013 2:58 pm
I think it would be helpful to have a command to handle name clashes regard the names of modules. I suggest the command RenameModule oldname, newname.
When used this would require module 'oldname' be referred to only as 'newname'. This means oldname::x would become newname::x and UseModule oldname would be UseModule newname. This would allow dealing with the name clash at the point where the module's code is included and not in the code itself (in case it is used elsewhere).
The second case is allowing a module to use an alias. I suggest the command ImportModule original_name As alias_name. This would allow a module to be referenced by both new and old portions of a program. Old code (or interfacing) could interact with the the module as alias_name while newer code might interact with it by referring to it under its original_name (also accessing any additional features that were added).
In time, a better example may present itself after the use of modules has gained some traction in the toolbox of PureBasic programmers. In the meantime I cobbled together some code to try and illustrate some use for these commands.
The code is meant to be treated as showing the contents of 4 separate files, 3 of which contain modules. It is a contrived example. I am aware that their may be other solutions to this problem that can be accomplished with structuring the code in various ways. The code can't be run because the commands to do so aren't implemented yet
. Even so, it would only draw a pattern on an image (but not display or save it).
I'm sure there will be opinions about whether this is a good idea or not. Please feel free to say what you like or hate about the idea and well see what becomes of it. 
When used this would require module 'oldname' be referred to only as 'newname'. This means oldname::x would become newname::x and UseModule oldname would be UseModule newname. This would allow dealing with the name clash at the point where the module's code is included and not in the code itself (in case it is used elsewhere).
The second case is allowing a module to use an alias. I suggest the command ImportModule original_name As alias_name. This would allow a module to be referenced by both new and old portions of a program. Old code (or interfacing) could interact with the the module as alias_name while newer code might interact with it by referring to it under its original_name (also accessing any additional features that were added).
In time, a better example may present itself after the use of modules has gained some traction in the toolbox of PureBasic programmers. In the meantime I cobbled together some code to try and illustrate some use for these commands.
The code is meant to be treated as showing the contents of 4 separate files, 3 of which contain modules. It is a contrived example. I am aware that their may be other solutions to this problem that can be accomplished with structuring the code in various ways. The code can't be run because the commands to do so aren't implemented yet

Code: Select all
;file 1 "triangle.pbi"
DeclareModule triangle
Global color = RGB(30, 255, 255)
Declare draw(x, y, length) ;draws triangle with sides of (length) from top (centered) point (x, y)
EndDeclareModule
Module triangle
Procedure draw(x, y, length) ;draws triangle with sides of (length) from top (centered) point (x, y)
Protected height = length * Sin(30)
Line(x, y, length / 2, height, color) ;right
Line(x - (length / 2) - 1, y + height - 1, length, 1, color) ;bottom
Line(x, y, -length / 2, height, color) ;left
EndProcedure
EndModule
;=================================
;file 2 "box.pbi"
DeclareModule box
Global color = RGB(255, 255, 30)
Declare draw(x, y, length) ;draw box with sides of (length / 2) from top-left corner (x, y)
EndDeclareModule
Module box
Procedure draw(x, y, length) ;draw box with sides of (length / 2) from top-left corner (x, y)
length / 2
Line(x, y, 1, length, color) ;left
Line(x, y + length - 1, length, 1, color) ;bottom
Line(x + length - 1, y, 1, length, color) ;right
Line(x, y, length, 1, color) ;top
EndProcedure
EndModule
;=================================
;file 3 "my_box.pbi"
DeclareModule box
Global color = RGB(255, 30, 255)
Declare draw(x, y, length) ;draw box with sides of (length) from top-left corner (x, y)
EndDeclareModule
Module box
Procedure draw(x, y, length) ;draw box with sides of (length) from top-left corner (x, y)
Line(x, y, 1, length, color) ;left
Line(x, y + length - 1, length, 1, color) ;bottom
Line(x + length - 1, y, 1, length, color) ;right
Line(x, y, length, 1, color) ;top
EndProcedure
EndModule
;=================================
;start of the main file, "main.pb"
;Normally select only one of the modules and change the line with "ImportModule ... As shape" to refer to it
Include "triangle.pbi"
ImportModule triangle As shape ;Now the module is known as both 'triangle' and 'shape' going forward.
;
;Routines that are designed for a module named 'shape' will operate without further tailoring.
; The alternative would be to edit the code each time the UseModule is used, or to replace the full
;module named (i.e. replace triangle::draw() with box::draw()) everywhere it is used. Alternatively
;the module being added would have to be renamed so that it matches with the code that uses it.
; If ImportModule ... As ... is used instead it would allow code to be tailored by swapping some modules
;in and out as long as their public members were the same. This would allow the module name to carry
;some distinguising elements.
UseModule shape
Define i
CreateImage(1, 200, 200, 24, 0)
StartDrawing(ImageOutput(1))
For i = 1 To 15
draw(i * 10, i * 10, 40)
Next
StopDrawing()
UnuseModule shape
;-----------------------------------------------
;Another issue, name clashes ...
;Include "box.pbi"
;;If both modules box and my_box were included we would need to rename one of them to avoid a name clash.
;RenameModule box, small_box ;Now the box module would be known only as small_box.
;Include "my_box.pbi"
