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
+3-5Lines changed: 3 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,14 @@
1
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
2
```pluto
3
3
local t = { "a", "b", "c" }
4
-
print(t:concat())
4
+
print(t:concat()) --> abc
5
5
```
6
6
In this code, the following occurs:
7
7
1.`concat` key is not directly present in the table, so `__index` is queried.
8
8
2.`__index` returns nil.
9
9
3. Since `__index` returned nil and this lookup is being performed by method invocation syntax (`:`), `__mindex` is queried.
10
10
11
-
Another example:
12
-
```pluto
11
+
```pluto title="An example showing how __mindex is walked recursively so :insert still works with a custom __mindex"
13
12
local t = setmetatable({}, {
14
13
__mindex = {
15
14
function sum()
@@ -27,8 +26,7 @@ print(t.sum) --> nil
27
26
print(t:sum()) --> 3
28
27
```
29
28
30
-
Beware of a caveat:
31
-
```pluto
29
+
```pluto title="An example of __index taking precedence"
0 commit comments