Allow interfaces as return types
Posted: Sat Jul 14, 2018 10:18 am
Interfaces can already be assigned by their object reference for variables:
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:
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:
But we cannot return these references to call methods on them:
We are already able to call procedures from structures through the use of prototypes:
But this is not an alternative since it does not pass the object instance and structure pointers cannot be declared as return types anyways.
Code: Select all
Interface SomeObject
EndInterface
obj.SomeObject=0
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()
We can already assign references to objects:
Code: Select all
obj.SomeObject=*instance
Code: Select all
Procedure.SomeObject ProcA()
; [Error] A structure can't be used with ProcedureReturn.
ProcedureReturn *instance
EndProcedure
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()