AliasModule to re-declare module names with an alias

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

AliasModule to re-declare module names with an alias

Post by Mistrel »

Lets say that I have various Shape modules that implement the types long, float, and double for 2D and 3D shapes, resulting in 6 modules. I also have another set of modules for different kinds of shapes such as Triangle, Rectangle, Pentagon, Octagon, Polygon, etc.

We cannot declare modules within other modules:

Code: Select all

DeclareModule Shape2i
  DeclareModule Triangle2i
  EndDeclareModule
EndDeclareModule
This is something that I wish we could do. But that only solves half the problem. Sometimes I want a module in scope but under a different name.

For example, lets say that modules Shape2i and Triangle2i are defined in the global scope. Once I decide I want "2i" shapes, I shouldn't need to refer to the primitive type again. Therefore I would define Shape2i as such:

Code: Select all

DeclareModule Shape2i
  AliasModule Triangle Triangle2i
EndDeclareModule
This would allow me to access "Triangle" from within Shape2i as Shape2i::Triangle.

Then in the global scope, once I decide which shape I want to use for my program, I can alias it there too, providing access with a typeless naming scheme.

Code: Select all

AliasModule Shape Shape2i

Shape::Triangle::new()
This is sort of a way to fake generics/templates with information hiding through aliases. The major difference being that we would still have to not only implement every type we wish to support explicitly, but also use the AliasModule keyword to bring it into scope.