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

How to make a function using a semi-colon instead of a full-stop

Started by _removed, 15 April 2015 - 02:17 PM
_removed #1
Posted 15 April 2015 - 04:17 PM
I have been wondering this for a long time, but how do you define a function that uses a semi-colon instead of a full stop?
Lupus590 #2
Posted 15 April 2015 - 04:27 PM
I believe this is correct


--#this
function t.func(self,parms)
  --code
end
--#is the same as this
function t:func(parms)
  --code
end

--#if you want this to be something loadable in an api
function func(self, parms)
  --code
end

you only want to do this if the function is in a table and needs to have access to something that is in its parent table, the parent table will be passed to it as the first parameter, if the function is declared as "t:func" then the table is named "self"
Edited on 15 April 2015 - 02:30 PM
KingofGamesYami #3
Posted 15 April 2015 - 06:09 PM
A semicolon is this ( ; ), if you are talking about OOP you'd use a colon ( : ).
_removed #4
Posted 15 April 2015 - 06:44 PM
Oops! Silly mistake! Yeah i mean a :
KingofGamesYami #5
Posted 15 April 2015 - 07:31 PM
In that case, there's really no reason to do so unless you are using OOP. For example, what Lupas posted *will* work, but it has no additional functionality.

OOP looks something like this:

local animals = {
  function eat( self, food )
    self.foodBar = self.foodBar + food
  end,
  function say( self )
    print( self.name )
  end,
}

local function monkey()
  return setmeatable( {name="monkey", food=10}, {__index = animals} )
end

local monkeyone = monkey()
monkeyone:eat( 1 )
monkeyone:say()
Bomb Bloke #6
Posted 15 April 2015 - 11:50 PM
It may help to mention that "self" refers to the table the function is in. So when calling, t:func(1,2,3) is simply shorthand for t.func(t,1,2,3).
_removed #7
Posted 02 May 2015 - 03:54 PM
Thanks for that, I'm learning OOP so I can make cleaner code.