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:
local function assert( cdn, msg, lvl )
if not cdn then
error(msg, lvl + 1)
end
return cdn
end
local function createButton( x, y, width, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
assert(type(x) == "number", "expected number", 3)
assert(type(y) == "number", "expected number", 3)
assert(type(width) == "number", "expected number", 3)
assert(type(height) == "number", "expected number", 3)
assert(type(text) == "string", "expected string", 3)
assert(type(placeholderText) == "string", "expected string", 3)
assert(type(func) == "function", "expected function", 3)
assert(type(backColorNormal) == "number", "expected number", 3)
assert(type(textColorNormal) == "number", "expected number", 3)
assert(type(backColorSelected) == "number", "expected number", 3)
assert(type(textColorSelected) == "number", "expected number", 3)
--# create it all here
end
function newButton( x, y, text, func )
createButton( x, y, #text + 2, 3, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end
function newButton( x, y, height, text, func )
createButton( x, y, #text + 2, height, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end
function newButton( x, y, text, func, backColorNormal, textColorNormal )
createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end
function newButton( x, y, height, text, func, backColorNormal, textColorNormal )
createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end
function newButton( x, y, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end
function newButton( x, y, height, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end
function newButton( x, y, text, placeholderText, func )
createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end
function newButton( x, y, height, text, placeholderText, func )
createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end
function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal )
createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end
function newButton( x, y, height, text, placeholderText, func, backColorNormal, textColorNormal )
createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end
function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end
function newButton( x, y, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end
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.