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

Overloading functions

Started by H4X0RZ, 29 April 2016 - 10:03 PM
H4X0RZ #1
Posted 30 April 2016 - 12:03 AM
A long time passed since I posted something useful on here. But today I present you my very own implementation of function overloading. I know this already exists and stuff, but it was a nice thing to do and I learned some stuff from it :3

Although you could do all this using many if clauses in one function this looks much cleaner in my opinion.

It features a (IMO) neat syntax. The only drawback is that the function is bound to a table.

Without talking too much, let's look at an example:

local overload = dofile("path/to/overload.lua")

overload "example" (
  function(str)
	print("Hello"..str)
  end
) {"string"}

overload.example(", world!")

--> Hello, world!
(yes, this is valid lua syntax)
I hope this example is self-explanatory.

For everyone interested, here is the source.


Greetings,
~H4X0RZ
Edited on 03 July 2016 - 07:50 PM
DannySMc #2
Posted 17 May 2016 - 01:00 PM
Hello, so I had a little Google of this to see what function overloading was but this doesn't seem to be it? I am a little confused? How does your example show function overloading? Or is it a different term from what I have read?

Function overloading is where you can define two functions under the same name, but giving them some kind of uniqueness example a parameter list? https://en.wikibooks.org/wiki/Computer_Programming/Function_overloading

Sorry I don't mean to be rude or anything I just don't understand this.
H4X0RZ #3
Posted 17 May 2016 - 02:17 PM
Hello, so I had a little Google of this to see what function overloading was but this doesn't seem to be it? I am a little confused? How does your example show function overloading? Or is it a different term from what I have read?

Function overloading is where you can define two functions under the same name, but giving them some kind of uniqueness example a parameter list? https://en.wikibooks.org/wiki/Computer_Programming/Function_overloading

Sorry I don't mean to be rude or anything I just don't understand this.

True, the example is "a bit" misleading. I'll expand it for you:


local overload = dofile("path/to/overload.lua")

overload "example" (
  function(str)
	print("Hello"..str)
  end
) {"string"}

overload "example" (
  function(arg1, arg2)
    print(arg1*arg2)
  end
) {"number", "number"}

overload.example(", world!")
--> Hello, world!

overload.example(5,5)
--> 25
DannySMc #4
Posted 17 May 2016 - 09:39 PM
Hello, so I had a little Google of this to see what function overloading was but this doesn't seem to be it? I am a little confused? How does your example show function overloading? Or is it a different term from what I have read?

Function overloading is where you can define two functions under the same name, but giving them some kind of uniqueness example a parameter list? https://en.wikibooks.org/wiki/Computer_Programming/Function_overloading

Sorry I don't mean to be rude or anything I just don't understand this.

True, the example is "a bit" misleading. I'll expand it for you:


local overload = dofile("path/to/overload.lua")

overload "example" (
  function(str)
	print("Hello"..str)
  end
) {"string"}

overload "example" (
  function(arg1, arg2)
    print(arg1*arg2)
  end
) {"number", "number"}

overload.example(", world!")
--> Hello, world!

overload.example(5,5)
--> 25

Ahh that's better, thank you! I shall have a look tonight, may test this out :D/>