Wishful thinking: ideas for PureBasic language improvements

Everything else that doesn't fall into one of the other PB categories.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Wishful thinking: ideas for PureBasic language improvements

Post by Mistrel »

I doodled up some of my feature requests as a showcase of what it might look like in PureBasic. This is a couple of modules that define shapes with some interfaces to access data. Note that some blocks of code are missing such as the the implementation of procedures. Everything for modules have also been kept in DeclareModule and not broken up into DeclareModule/Module block pairs for simplicity.

Modules:

Code: Select all

;-- Geometry

DeclareModule Geometry
  Structure Point2i
    x.l
    y.l
  EndStructure
  
  Structure Point2f
    x.f
    y.f
  EndStructure
EndDeclareModule

;-- Polygon

DeclareModule Polygon2i
  #MaxSize=$FFFFFFFF
  
  Structure Point Extends Geometry::Point2i
  EndStructure
  
  Macro POINT_ARRAY
    Point[]
  EndMacro
  
  Interface IObject
    Declare.l size()
    Declare.*POINT_ARRAY toPoints()
    Declare draw()
  EndInterface
  
  Declare.IObject fromPointXY(i...)
EndDeclareModule

DeclareModule Polygon2f
  #MaxSize=$FFFFFFFF
  
  Structure Point Extends Geometry::Point2f
  EndStructure
  
  Macro POINT_ARRAY
    Point[]
  EndMacro
  
  Interface IObject
    Declare.l size()
    Declare.*POINT_ARRAY toPoints()
    Declare draw()
  EndInterface
  
  Declare.IObject fromPointXY(f...)
EndDeclareModule

;-- Triangle

DeclareModule Triangle2i
  #Size=3
  
  ModuleAlias Polygon Polygon2i
  
  Structure Point Extends Geometry::Point2i
  EndStructure
  
  Macro POINT_ARRAY
    Point[#Size]
  EndMacro
  
  Interface IObject
    Declare.l size()
    Declare.*POINT_ARRAY toPoints()
    Declare.Polygon::IObject toPolygon()
  EndInterface
  
  Declare.IObject fromPointXY(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
EndDeclareModule

DeclareModule Triangle2f
  #Size=3
  
  ModuleAlias Polygon Polygon2f
  
  Structure Point Extends Geometry::Point2f
  EndStructure
  
  Macro POINT_ARRAY
    Point[#Size]
  EndMacro
  
  Interface IObject
    Declare.l size()
    Declare.*POINT_ARRAY toPoints()
    Declare.Polygon::IObject toPolygon()
  EndInterface
  
  Declare.IObject fromPointXY(x1.f, y1.f, x2.f, y2.f, x3.f, y3.f)
EndDeclareModule

;-- Shape

DeclareModule Shape2i
  ModuleAlias Triangle Triangle2i
  ModuleAlias Polygon Polygon2i
  
  Structure Point Extends Geometry::Point2i
  EndStructure
  
  Interface ITriangle Extends Triangle::ITriangle
  EndStructure
EndDeclareModule

DeclareModule Shape2f
  ModuleAlias Triangle Triangle2f
  ModuleAlias Polygon Polygon2f
  
  Macro POINT_ARRAY
    Polygon::POINT_ARRAY
  EndMacro
  
  Structure Point Extends Geometry::Point2f
  EndStructure
  
  Interface ITriangle Extends Triangle::ITriangle
  EndStructure
EndDeclareModule
Key points:
  • Procedures can return pointers to arrays and structures, structure copies, and other interfaces
  • Modules inherit scope
  • ModuleAlias provides a way to re-declare a module with a different name
  • Modules can contain other modules (declared or with ModuleAlias)
  • Variadic arguments for procedures
Example code (simplified):

Code: Select all

ModuleAlias Shape Shape2i

Define triangle.Shape::ITriangle

triangle=Shape::Triangle::fromPointXY(50,2, 98,86, 2,86)

Define pointArray.Shape::POINT_ARRAY
Define x
Define y

For i=1 To triangle\size()
  pointArray=triangle\toPoints()
  
  x=pointArray[i]\x
  y=pointArray[i]\y
  
  Debug Str(x)+" "+Str(y)
  
  x=triangle\toPoints()[i]\x
  y=triangle\toPoints()[i]\x
  
  Debug Str(x)+" "+Str(y)
Next i

triangle\toPolygon()\draw()
Key points:
  • Support for variable arrays without the need for a structure
  • Accessing an array returned from an interface
  • Chain calling procedures from their return values: a()\b()\c()...
Please note that this is NOT an example of object-oriented programming. Instead it demonstrates how to leverage paradigms already present in PureBasic which can be used for any purpose; the use of interfaces here is just one possible use case.