Allow interfaces as return types

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

Allow interfaces as return types

Post by Mistrel »

Interfaces can already be assigned by their object reference for variables:

Code: Select all

Interface SomeObject
EndInterface

obj.SomeObject=0
But objects are very poorly supported where they matter most: as return types. PureBasic passes objects around as pointers and therefore all objects are already passed by reference. I see no reason why we shouldn't have the ability to do return these references from a procedure and I was surprised to find that we didn't already have this functionality.

I know that objects are less than second-class in PureBasic and object-oriented programming is not supported. But this is a very minor feature that will go a long way for veterans looking to push this language to the very edge. I would love to use PureBasic more but I continue to find it difficult to port code to PureBasic, which limits its exposure in my work.

Here is a specific example of what I would like to be able to do:

Code: Select all

Interface BoxObject
  getWidth()
EndInterface

Interface SomeObject
  ; [Error] A structure can't be used with ProcedureReturn.
  getBox.BoxObject()
EndInterface

obj.SomeObject= ...

Debug obj\getBox()\getWidth()
This error doesn't make sense. We're not returning a structure. We're returning a reference to an interface which is nothing more than a pointer.

We can already assign references to objects:

Code: Select all

obj.SomeObject=*instance
But we cannot return these references to call methods on them:

Code: Select all

Procedure.SomeObject ProcA()
  ; [Error] A structure can't be used with ProcedureReturn.
  ProcedureReturn *instance
EndProcedure
We are already able to call procedures from structures through the use of prototypes:

Code: Select all

Procedure SayHello()
  Debug "Hello!"
EndProcedure

Prototype.i ProtoSayHello()

Structure VTable
  SayHello.ProtoSayHello
EndStructure

a.VTable
a\SayHello=@SayHello()

; Works fine
a\SayHello()
But this is not an alternative since it does not pass the object instance and structure pointers cannot be declared as return types anyways.