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

Calling function from another function in an API

Started by TheUltimateKrtekCZ, 29 October 2017 - 11:52 AM
TheUltimateKrtekCZ #1
Posted 29 October 2017 - 12:52 PM
Hello, guys.

I have a little problem with calling a function inside a function from the same API.

system/APIs/peripheralAPI:

function getPeripheralSideByType(peripheralType)
  return getPeripheralSideByType(peripheralType, count)
end

function getPeripheralSideByType(peripheralType, count)
  local peripheralFound = 0
  for i = 1, 6, 1 do
    if peripheralType == peripheral.getType(rs.getSides()[i]) then
     peripheralFound = peripheralFound + 1
     if peripheralFound == count then
      return rs.getSides()[i]
     end
    end
  end
  return nil
end

function getPeripheralByType(peripheralType)
  return getPeripheralByType(peripheralType, 1)
end

function getPeripheralByType(peripheralType, count)
  if getPeripheralSideByType(peripheralType, count) == nil then return nil
    else return peripheral.wrap(getPeripheralSideByType(peripheralType, count))
  end
end

test:

peripheralAPI.getPeripheralByType("monitor", 1)
-- returns table
peripheralAPI.getPeripheralByType("monitor")
-- returns nil

Pls Help!
supernicejohn #2
Posted 29 October 2017 - 08:02 PM
Basically, function overloading isn't a feature in CC, however there are a couple implementations, http://www.computercraft.info/forums2/index.php?/topic/26606-overloading-functions/ being the first to turn up in a google search.
If you don't want to use an api, you could have these checks inside the main functions themselves as there is nothing wrong with passing nil as function arguments (with incorrect # of arguments resulting in nil).
What happens when you run that file is that a global function getPeripheralByType gets created, which accepts 1 argument, then after that, it is overwritten by a function of the same name that accepts 2 arguments. The same is true for getPeripheralSideByType , after which you attempt to call the 2-argument function with one argument. Nothing wrong really, but in this case it's not what you want.

For example, in the getPeripheralSideByType function, you could place "count = count or 1" as a first line, and it will use '1' whenever a 'count' isn't specified.
Hope that helps!
KingofGamesYami #3
Posted 29 October 2017 - 08:02 PM
I'm not sure how that result could occur, assuming everything you've said is correct. Totally missed that you were overloading a function. Sorry!

That said, there is a function which can replicate most of the functionality you're implementing.
Edited on 29 October 2017 - 07:03 PM
Bomb Bloke #4
Posted 30 October 2017 - 03:44 AM
Basically, function overloading isn't a feature in CC

More specifically, it's not a feature of Lua.

When you define a function in the manner done in the OP, what you're doing is assigning a function pointer to a variable - much the same as when you assign a number or a string or whatever. If you consecutively assign two different functions to the one variable, then the first gets lost and only the second remains.

If you want two different functions, then you're going to have to assign them to two different variables. However, to expand on what supernicejohn was saying, Lua also doesn't care how many arguments you pass to a function: it's up to the function's code to handle or ignore however many arguments you want. For eg, you can do:

function example(arg)
   if arg then
      print("An argument was passed.")
   else
      print("No argument was passed.")
   end
end

example("Hello")   --# Prints "An argument was passed."
example()          --# Prints "No argument was passed."

Then there's the awesomeness that is the vararg function:

function example(...)
   print("I received " .. arg.n .. " arguments.")
end

example()        --# "I received 0 arguments."
example(1,2,3)   --# "I received 3 arguments."
example("4", 5)  --# "I received 2 arguments."