Recognized Interface returned by Interface Methods

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Recognized Interface returned by Interface Methods

Post by grabiller »

Hi,

When defining an Interface, the compiler does not accept a method returning the same interface. It says "a Procedure can't return a Structure".

But an instanciated Interface is a pointer.

Of course we can work around by declaring de method returning a 'i'. But this way we can't chain methods calls, the compiler won't recognize those and will generate an error.

For instance :

Code: Select all

; with:
Interface MyClass
  DoSomething.MyClass()
  DoSomethingElse.MyClass()
EndInterface

oC.MyClass = New_MyClass() ; granted you've created this Procedure to create the instance.

; then we could do something like that:
oC\DoSomething()\DoSomethingElse()

; instead of
oC\DoSomething()
oC\DoSomethingElse()

Or perhaps it's allready possible and I've missed something ?

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Recognized Interface returned by Interface Methods

Post by srod »

Ah well, that code you've posted doesn't make sense unless the \DoSomething() method returns another instance of the same class? You are essentially correct though in that this kind of thing is not possible.

The thing is of course that PB would need to step up its garbage collection before it could offer this kind of 'chaining' of method calls etc. Not sure that Fred would want to go down that path.
I may look like a mule, but I'm not a complete ass.
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Recognized Interface returned by Interface Methods

Post by grabiller »

srod wrote:Ah well, that code you've posted doesn't make sense unless the \DoSomething() method returns another instance of the same class?../..
Exactly, that's why in my exemple 'DoSomething()' is declared this way in the 'MyClass' interface: DoSomething.MyClass()

It means it is supposed to return an instance of the same class. But the compiler 'thinks' the method would return a Structure, so it does not allow this syntax.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Recognized Interface returned by Interface Methods

Post by BorisTheOld »

srod wrote: The thing is of course that PB would need to step up its garbage collection before it could offer this kind of 'chaining' of method calls etc. Not sure that Fred would want to go down that path.
Reference counting and garbage collection can be handled already, but it needs a few lines of code added to each class. Also, objects need to be created, destroyed, and cloned in a particular way.

Here's what a typical application looks like - DvsML06 is the class name of the application:

Code: Select all

.
.
an IncludeFile for each class that is used in the application
.
.
  GlobalObject(gloDvsML06, objDvsML06)
  gloDvsML06 = CreateObject(DvsML06) (gloDvsML06)
  ObjectCall(gloDvsML06, Main) ()
  gloDvsML06 = ObjectReturn(gloDvsML06, Destroy) ()

End
This translates to:

Code: Select all

  Global gloDvsML06.objDvsML06
  gloDvsML06 = clsDvsML06_funCreate(gloDvsML06)
  gloDvsML06\subMain()
  gloDvsML06 = gloDvsML06\funDestroy()
The Create, Destroy, and Clone methods of a class do all the memory management, and only takes about 50 lines of code per class. And except for the above code, all other application code is written in the form of classes.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply