This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
agowa338's profile picture

Function Overloading

Started by agowa338, 05 April 2015 - 10:02 PM
agowa338 #1
Posted 06 April 2015 - 12:02 AM
If I do this i get a "java.lang.arrayindexoutofboundsexception" error, so whats the proper way to do this?

function doSomething(parameter)
	...
end
function doSomething()
	doSomething(default_parameter)
end

EDIT: Yes, it was JAVA syntax, now it is LUA ;)/> (like it was intended to be)
Edited on 05 April 2015 - 10:41 PM
Lupus590 #2
Posted 06 April 2015 - 12:18 AM
why is this forum thread tagged as lua when you are using java?
or if this is lua

function doSomething(parameter)
   parameter = parameter or default_parameter
   ...
end
KingofGamesYami #3
Posted 06 April 2015 - 12:24 AM
Assuming you were using the correct syntax while testing, arrayindexoutofboundsexception is triggered if a function calls itself more than 256 times. Which you are managing to do, since your doSomething( parameter ) is overwritten by your declaration of doSomething()
InDieTasten #4
Posted 06 April 2015 - 12:25 AM
why is this forum thread tagged as lua when you are using java?
or if this is lua

function doSomething(parameter)
   parameter = parameter or default_parameter
   ...
end
Your example fixes the problem, the array itself comes from java, since he's calling his own function recursively, unteil the callstack reaches it's limit of 256(?)
Just to comment about your solution:
In lua, you cannot have multiple overloads to one function. The latter overrides always the previous.
Another aspect of the funcionality is, that it is perfectly fine to pass nothing(nil) to a function, even if it's definition contains one.
The functions itself need to check for their inputs and need to take actions(exceptions, or different/default behaviour) on specific inputs.
Lupus590 #5
Posted 06 April 2015 - 12:32 AM

function doSomething(parameter)
   parameter = parameter or default_parameter
   ...
end
In lua, you cannot have multiple overloads to one function. The latter overrides always the previous.
Another aspect of the funcionality is, that it is perfectly fine to pass nothing(nil) to a function, even if it's definition contains one.
The functions itself need to check for their inputs and need to take actions(exceptions, or different/default behaviour) on specific inputs.

either I have not understood any of this, or my example covered all this
agowa338 #6
Posted 06 April 2015 - 12:39 AM
why is this forum thread tagged as lua when you are using java?
or if this is lua

function doSomething(parameter)
   parameter = parameter or default_parameter
   ...
end


I've used the wrong syntax for this post, sorry :(/> . I normally write in Java or vb.net.
But thanks for your answer.