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

What does __ (double underscore) mean/do?

Started by Xenthera, 09 June 2013 - 01:53 PM
Xenthera #1
Posted 09 June 2013 - 03: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)
Engineer #2
Posted 09 June 2013 - 03: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/wiki/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 ;)/>
Sorroko #3
Posted 09 June 2013 - 03: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 :)/>
ironwiccan #4
Posted 09 June 2013 - 04:11 PM
I was wondering the same thing! I know it wasn't my question but thanks!
theoriginalbit #5
Posted 10 June 2013 - 03:49 AM
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__`.
Sorroko #6
Posted 10 June 2013 - 07:01 AM
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.uk/lab/cplus/c++.rules/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.
Bubba #7
Posted 10 June 2013 - 10:03 AM
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.uk/lab/cplus/c++.rules/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.
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
MaHuJa #8
Posted 10 June 2013 - 12:10 PM
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.

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.
Bubba #9
Posted 10 June 2013 - 01:19 PM
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.
Imque #10
Posted 11 June 2013 - 08: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.