Jump to content




What does __ (double underscore) mean/do?


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

#1 Xenthera

  • Members
  • 170 posts

Posted 09 June 2013 - 02:53 PM

I've seen it used a lot and i'm not exactly sure what it does.

Ex:

__Index (or something to that nature)

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 09 June 2013 - 02:59 PM

These are metamethods for metatables. See those as actions, because they mostly fire when you do certain stuff with a table.
Here is a list: http://lua-users.org...MetatableEvents

Here a bit of sample code;
local t = {}
setmetatable( t, {
   __newindex = function( t, k, v )
        error('cant edit table', 2)
   end
})
try this code out and try to set a new value to t ;)

#3 Sorroko

  • Members
  • 87 posts

Posted 09 June 2013 - 02:59 PM

In general "__" are meant to indicate 'private' variables and are mainly a coding style/practice, however in the case of "__index" it is a metamethod related to meta tables, you can read about the methods here: http://lua-users.org...methodsTutorial

EDIT: Frikin' ninja'd :)

#4 ironwiccan

  • Members
  • 78 posts

Posted 09 June 2013 - 03:11 PM

I was wondering the same thing! I know it wasn't my question but thanks!

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 June 2013 - 02:49 AM

View PostSorroko, on 09 June 2013 - 02:59 PM, said:

In general "__" are meant to indicate 'private' variables and are mainly a coding style/practice
I'm most programming languages the convention for private variables or properties of objects is a single `_` not double. The double `__` in all languages that I have seen it in relates to metamethods. For example Python also has metamethods defined like so `__init__`.

#6 Sorroko

  • Members
  • 87 posts

Posted 10 June 2013 - 06:01 AM

Quote

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

From http://www.doc.ic.ac...ules/chap5.html

Most languages I know use it as a private 'not meant to be accessed' variable/function, however you are probably right for languages such as lua and python.

#7 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 June 2013 - 09:03 AM

View PostSorroko, on 10 June 2013 - 06:01 AM, said:

Quote

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

From http://www.doc.ic.ac...ules/chap5.html

Most languages I know use it as a private 'not meant to be accessed' variable/function, however you are probably right for languages such as lua and python.

Private and not to be accessed are not really the same thing though.

Quote

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

I always use a single underscore for variables that are taken by functions. e.g
local function myFunction(_underscore, _var)
  --#No collisions here!
end

local regularVar = 5


#8 MaHuJa

  • Members
  • 26 posts

Posted 10 June 2013 - 11:10 AM

Quote

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.
Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

As I remember it, __x means some x defined by the standard, _x is to be used for compiler-specific (and its libraries) identifiers. But that's C.

Quote

I always use a single underscore for variables that are taken by functions. e.g
local function myFunction(_underscore, _var)
  --#No collisions here!
end

local regularVar = 5

You may as well drop them. The only reason to have "_var" is if you'll be accessing "var" in the environment. As in, you're using both var and _var in the function, in which case _var should probably be called something else entirely. And even if they're both called var, you can explicitly refer to the one in the environment by going through the environment table.



#9 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 10 June 2013 - 12:19 PM

View PostMaHuJa, on 10 June 2013 - 11:10 AM, said:

Quote

I always use a single underscore for variables that are taken by functions. e.g
local function myFunction(_underscore, _var)
  --#No collisions here!
end

local regularVar = 5

You may as well drop them. The only reason to have "_var" is if you'll be accessing "var" in the environment. As in, you're using both var and _var in the function, in which case _var should probably be called something else entirely. And even if they're both called var, you can explicitly refer to the one in the environment by going through the environment table.

You misinterpret my reasoning entirely. I use the underscore for my own organization and so I can quickly tell if something belongs to the function or to the global environment. It has nothing to do with variable overriding.

#10 Imque

  • Members
  • 134 posts

Posted 11 June 2013 - 07:43 AM

I'll just say now from experience and a lot of the other experienced forum members may agree with me in saying this isn't just something you'll learn overnight. It toke me a bit. I learnt about them by simply looking at some code it's been used in.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users