How to make a wrapper of same named function/command?

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

How to make a wrapper of same named function/command?

Post by Bugala »

in effort to keep code more readable, I am trying to make my debug stuff in to separate place in code that can be added/removed with copy-paste.

Idea is following:

Code: Select all

function myfunc(var)
debugprint("var")
endfunction


function myfunc(var)
debugprint("myfunc("..var..")")
myfunc(var)
endfunction
Idea being that I am trying to replace the actual function with a same named function that would do some debugstuff and then call the original function to actually do the function.

The above codes problem is that instead of calling the original version of that function, it will call the new version of function, making it getting into infinite recursive loop.

To make this problem even more difficult is that many functions that I want to use this system at, are also using self. As example: "MyFuncs:MyPrint(var)"

Is this doable somehow in Hollywood, and how?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: How to make a wrapper of same named function/command?

Post by SamuraiCrow »

Hi Bugala!

The closest you can get is either to define 2 versions of the same function using preprocessor macros that check for a constant declared on the command line or 2 versions of an additional function that can be called in the beginning or end of the debugger version of the original using the same type of preprocessor technique. Other than that, there are no ways to make 2 functions exist concurrently with the same name without embedding one or the other or both in tables to separate the "namespace" of the definition.

Since I have a few minutes, I'll give an example combining all the techniques I mentioned:

Code: Select all

@IF #DEBUGMODE
debugmode={}
@ENDIF
nodebugmode={}
Function nodebugmode.blabla()
	var=1
EndFunction

@IF #DEBUGMODE
Function debugmode.blabla()
	nodebugmode.blabla()
	DebugPrint("var="..var)
EndFunction

blabla=debugmode.blabla
@ELSE
blabla=nodebugmode.blabla
@ENDIF
Then to activate debug mode:

Code: Select all

interpreter -setconstants "DEBUGMODE" progname.hws
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to make a wrapper of same named function/command?

Post by Bugala »

Thanks from idea @SamuraiCrow

It seems like that could work as I am wanting program execution wise, going to think about it a bit whether it makes the code too messy to read or not, as my main purpose is to find a solution that would keep the code reading as clean as possible, hence would like a solution where I only need to add lines to the end of the code. That everything looks normal until you reach the point the debug parts start.

I was able to figure out this a bit forward, the self part is still a problem for me.

Code: Select all

Function func(var)
	DebugPrint(var)
EndFunction

debug1 = func

Function func(var)
	DebugPrint("func("..var..")")
	debug1(var)
EndFunction

func("test")
This one works as expected. Or rather bit unexpected, since I would have actually expected this to end into infinite recursion, but it actually doesn't, and I am not even sure why, since it would make sense if Hollywood just made a copy of that function when using "newfunc = oldfunc", but actually it seems to be a reference since if I now change debug1 into something else, it will also reflect to new "func(var)" function.

@Andreas is this a bug that it doesn't end in endless recursion, or supposed to work this way, and Why? For shouldn't new "func(var)" first refer to debug1, which in turn refers to "func(var)" again, ending into endless recursion?


using SELF is still a problem, I tried like this:

Code: Select all

funcs = { var = 1}

Function funcs:func(var)
	DebugPrint(var)
	DebugPrint(self.var)
EndFunction



test = {}

test.test = funcs.func

Function funcs:func(var)
	DebugPrint("funcs:func()")
	test:test(var)
EndFunction

funcs:func("test")
And it works otherwise, except it is not able to reference to self.var, which is otherwise bit off, except when considering that the previous one doesnt get to endless recursion, reason is probably on that same reason.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: How to make a wrapper of same named function/command?

Post by jalih »

Bugala wrote: Thu Dec 17, 2020 1:56 pm

Code: Select all

Function func(var)
	DebugPrint(var)
EndFunction

debug1 = func

Function func(var)
	DebugPrint("func("..var..")")
	debug1(var)
EndFunction

func("test")
This one works as expected. Or rather bit unexpected, since I would have actually expected this to end into infinite recursion, but it actually doesn't, and I am not even sure why, since it would make sense if Hollywood just made a copy of that function when using "newfunc = oldfunc", but actually it seems to be a reference since if I now change debug1 into something else, it will also reflect to new "func(var)" function.

@Andreas is this a bug that it doesn't end in endless recursion, or supposed to work this way, and Why? For shouldn't new "func(var)" first refer to debug1, which in turn refers to "func(var)" again, ending into endless recursion?
To me, it looks like you are storing a reference to function "func()" and then re-declaring it later on. There is no recursive call, because "debug1" and "func" are not the same function.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to make a wrapper of same named function/command?

Post by Bugala »

So does it mean there are now two different func functions existing?

As in original func, to which Debug1 is referencing to, and a new version declared at later point?

And if this is so, then is it possible the original func disappears at some point, like in garbage collection or something, since basically, I guess it sort of doesn't exist anymore?

That to put it more technical.

Original Func resides in memory 0x01, while new func resides in memory spot 0x02.

Debug1 references to memory 0x01 (which at this point still contains the original func), but who says something else wouldn't occupy the 0x01 memory location, now that nothing is officially occupying it anymore?
Post Reply