You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/New Features/Mindex Metamethod.md
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,14 @@
1
-
The `__mindex` metatable field is accessed when a method call is attempted on a nil value.
1
+
The `__mindex` metamethod stands for 'method index'. It has a secondary priority to `__index` and it's only invoked when the lookup is being performed by method invocation syntax. This is used to avoid compatibility issues regarding Pluto's default metatable for tables. For example:
2
+
```pluto
3
+
local t = { "a", "b", "c" }
4
+
print(t:concat())
5
+
```
6
+
In this code, the following occurs:
7
+
1.`concat` key is not directly present in the table, so `__index` is queried.
8
+
2.`__index` returns nil.
9
+
3. Since `__index` returned nil and this lookup is being performed by method invocation syntax (`:`), `__mindex` is queried.
2
10
11
+
Another example:
3
12
```pluto
4
13
local t = setmetatable({}, {
5
14
__mindex = {
@@ -18,15 +27,10 @@ print(t.sum) --> nil
18
27
print(t:sum()) --> 3
19
28
```
20
29
21
-
Notice how the above sample uses `:insert` instead of `table.insert`, this works because:
22
-
- Pluto sets a default metatable on every table with `{ __mindex = _G.table }`
23
-
-`__mindex` is looked up recursively — in this case the metatable of our metatable has Pluto's default metatable.
24
-
25
-
Furthermore, note that `__mindex` is only checked after direct/raw access and `__index` lookups have yielded nil:
0 commit comments