How to make Function with : on a fly?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

How to make Function with : on a fly?

Post by Bugala »

I have this situation here (writing this longer than necessary to get the idea better)

Code: Select all

OOP = {}

Function OOP:NewInstance()
	o = {}
	o.variable = 1
	Return(o)
EndFunction

Function OOP:FuncToBeMadeLater()
EndFunction

Function OOP:NewInstance_Type1()
	o = OOP:NewInstance()
	o:FuncToBeMadeLater = Function() stuff EndFunction
	Return(o)
EndFunction

testOOP = OOP:NewInstance_Type1()
By other words I am first making a prototype OOP, and then there will come different types of OOPs that are going to be using that Prototype to get their stuff in place, but these different types will have a different "FuncToBeMadeLater" method, that I am planning on making at point I am making New Instance of Type1, Type2, Type3...

But now the problem is that I want to use the SELF system, which means I need to use : when declaring that. However, when I do that at O:FuncToBeMadeLater = Function() stuff EndFunction, it gives me an error.

If I use O.FunctoBeMadeLater instead, it works fine, except I dont have the self then.

Is there a way for me to do this in such a way that I can get the SELF there?
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to make Function with : on a fly?

Post by Bugala »

I realised I can do the function the traditional way inside the function to solve this problem:

Code: Select all

Function OOP:NewInstance_Type1()
	o = OOP:NewInstance()
	function O:MyFuncToBeMade()
	stuff
	endfunction
endfunction
But in case I need it, is there also a way to get it done in way of:

Code: Select all

O:MyFuncToBeMade = function() stuff endfunction
?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: How to make Function with : on a fly?

Post by SamuraiCrow »

You can pass self to a function explicitly as the first parameter. The : only implicitly adds the self parameter to contain the table it's a member of.

Code: Select all

o.fntoaddlater=Function(self) stuff EndFunction
I'm on registered MorphOS using FlowStudio.
Post Reply