theoriginalbit, on 08 April 2014 - 01:32 AM, said:
Graypup, on 07 April 2014 - 05:33 PM, said:
Who needs functional overloading? It can be done with some type() and some if statements.
I can become very verbose and annoying if there are too many arguments. Function overloading can make it a lot easier and cleaner. Take the following example:
--snip--
now go ahead and make something with the same functionality as above with if statements; it should be just as readable and contain no bugs/typos. k. go.
Speaking of annoyance, writing (and reading) 65536 different functions instead of 16 nested ifs doesn't seem very interesting... (Just an example, to make it obvious)
In addition, if statements are more flexible: sharing code chunks, checking arguments in different order (or in 2 orders at the same time), etc.
Everything that I "
would change", actually
can be changed from inside, that's the beauty of Lua. Currently I'm rewriting LÖVE framework (love2d.org) from inside, trying to completely remove callback mechanics from it, just because I don't like callbacks. Advice for theoriginalbit and all the others who want overloading: you can automate the process of "pseudo-overloading" by writing a function that checks argument types (call it "atype", for example) and using it like this:
function f(...)
local args = {...}
if atype(args, "string s1, number n, string s2") then
-- one function
elseif atype(args, "string s1, string s2, thread cor, number m") then
-- another function
else
error("Incorrect arguments to f")
end
end
Also, see that variable names inside the strings? So, when atype at last finds correct argument types, before returning true, it rearranges the "args" table, so all your arguments are availble as args.s1, args.n etc.
If you want the syntax of overloading, well, that's a little harder, but also possible! Hint:
newfunc("boolean f(string s1, number n, string s2)", function(arg)
-- your code
end)
If you think that's ugly, well, there is one last way: write an additional program with bunch of "string.gsub"s that translate java-like definitions into that.
Edited by 0099, 28 August 2014 - 01:33 PM.