Overloading Functions


Recommended Posts

Overloading a built in function like sin()? No. Those functions are actually compiled straight up and not stored in the psuedo-code as a symbol. You probably could override a built in object, but then you'd have no way of calling the base class. The exception would be, perhaps with objects of Local. For example, channel. You can do this:

class CChannel

function ListAll(y)

return (local.channel.ListAll() + "abc")

endfunction

endclass

endclass

and then:

global channel = new(CChannel)

and you'll have overridden the internal Channel object. This works because you can access that object in two ways, provided the default connection is set to Local. You can do: channel.listall(), or local.channel.listall(). By creating a global variable called "Channel" we bypass the internal channel object. In actual fact, you can still call other functions on the internal channel object, for example, channel.add(). The only one you can't is the one you overrode. But you can call it by adding Local. in front.

That all said, you are really just working around the system. I can't guarantee that this will work the same way in future revisions.

Link to comment
Share on other sites

No I don't want to overload the built-in functions. I'm talking about using overloading in my own functions. Like the pivot() function in the other thread for example; I'd like it to be able to work on either string or numeric arrays, but I have to type the arguments.

Link to comment
Share on other sites

Use variable arg notation. So, the removeAtCols() example becomes:


function removeAtCols()
try
private junk = arg0 + "a"
private string arr = arg0
catch()
private arr = arg0
endcatch
private arr = arg0
private index = arg1
private nCols = arg2

arr[][index] = arr[][index+nCols, numcols(arr)-1]
arr = arr[][0,numcols(arr)-1-nCols]
return arr
[/CODE]

The try / catch block is because doing arg0 + "a" when arg0 is a number will cause an error. If arg0 is a string, it won't.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.