Jump to content




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


  • You cannot reply to this topic
6 replies to this topic

#1 _removed

  • Members
  • 262 posts

Posted 15 April 2015 - 03: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?

#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 15 April 2015 - 03: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 by Lupus590, 15 April 2015 - 03:30 PM.


#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 April 2015 - 05:09 PM

A semicolon is this ( ; ), if you are talking about OOP you'd use a colon ( : ).



#4 _removed

  • Members
  • 262 posts

Posted 15 April 2015 - 05:44 PM

Oops! Silly mistake! Yeah i mean a :

#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 April 2015 - 06: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()


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 April 2015 - 10: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).

#7 _removed

  • Members
  • 262 posts

Posted 02 May 2015 - 02:54 PM

Thanks for that, I'm learning OOP so I can make cleaner code.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users