can i store functions attributes to use them later?

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

can i store functions attributes to use them later?

Post by Bugala »

Here is a piece of test code that does do what I want basically:

Code: Select all

functable = {}

Function func1 (a, b)
	DebugPrint(a)
	DebugPrint(b)
EndFunction

InsertItem(functable, {func=func1, attributes={5, 4} })

functable[0].func(functable[0].attributes[0], functable[0].attributes[1])
What I am trying to do, is to add functions to a table. Idea being that when i execute them, it will also include attributes for that function.

In case I am using wrong word here, what I mean here is:

Code: Select all

 function checklocation(x, y)
Function is the "Checklocation()" by Attributes I mean "x" and "y".

In best case, I could just do like this:

Code: Select all

FunctionTable[1]
And it would result "CheckLocation(100, 200)" being executed.

Second best case would be:

Code: Select all

FunctionTable[1](attributestable[1])
resulting in "CheckLocation(100, 200)" being executed.

That by other words, Attributestable[1] would contain "(100, 200)" inside it.

But is there any way to do it something like this, or do I have to resort into using the first, working example?
User avatar
Clyde
Posts: 349
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: can i store functions attributes to use them later?

Post by Clyde »

I am not sure, but I think that your "best case" is not possible.

The second version could work IMHO. I have no Hollywood here currently, so this code may not work, but you get the idea:

Code: Select all

InsertItem(functtable, func1)
InsertItem(attributes, {5, 4})

functtable[0](attributes[0])

BUT then you have to change the signature of func1 so that it takes just one argument:

Code: Select all

Function func1 (params)
	DebugPrint(params.a)
	DebugPrint(params.b)
EndFunction
But to be honest this seems not very handy or clear to read and understand.

I go with this approach. Not perfect, but better readable IMHO:

Code: Select all


Function _privateFunction(a, b)
    DebugPrint(a)
    DebugPrint(b)
EndFunction

funcTable = {

    publicFunction = Function(a, b)
        _privateFunction(a, b)
    EndFunction
}

; now you call it

funcTable.publicFunction(a, b)

Of course, _privateFunction is not really private it is just a organisational/conceptual definition. Another disadvantage of this is that you have to change the signature at 4 places if it has to be changed.

But I see more pros and cons. But if anybody has a better way of doing it (Allanon?), then I would be happy to know.
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
Post Reply