SteveMyres Posted April 7, 2013 Share Posted April 7, 2013 Is overloading of function definitions possible, for example to make an array handling function handle both numeric and string arrays? If so, is it only in a class or can it be done in a standard function? Link to comment Share on other sites More sharing options...
AzeoTech Posted April 7, 2013 Share Posted April 7, 2013 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 More sharing options...
SteveMyres Posted April 7, 2013 Author Share Posted April 7, 2013 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 More sharing options...
AzeoTech Posted April 7, 2013 Share Posted April 7, 2013 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 More sharing options...
AzeoTech Posted April 7, 2013 Share Posted April 7, 2013 For those wondering, here's the whole thread on removeAtCols(): http://www.azeotech.com/board/index.php?/topic/5110-myarrayremoveat-for-columns/page__p__18216#entry18216 Link to comment Share on other sites More sharing options...
SteveMyres Posted April 7, 2013 Author Share Posted April 7, 2013 Oh, that's right. I forgot that you don't have to explicitly prototype if you use the argn notation. Excellent method. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.