Skip to content

Commit 3ef01a5

Browse files
committed
Explain __mindex snippets a bit better
1 parent 02f8e18 commit 3ef01a5

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

docs/New Features/Mindex Metamethod.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
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:
22
```pluto
33
local t = { "a", "b", "c" }
4-
print(t:concat())
4+
print(t:concat()) --> abc
55
```
66
In this code, the following occurs:
77
1. `concat` key is not directly present in the table, so `__index` is queried.
88
2. `__index` returns nil.
99
3. Since `__index` returned nil and this lookup is being performed by method invocation syntax (`:`), `__mindex` is queried.
1010

11-
Another example:
12-
```pluto
11+
```pluto title="An example showing how __mindex is walked recursively so :insert still works with a custom __mindex"
1312
local t = setmetatable({}, {
1413
__mindex = {
1514
function sum()
@@ -27,8 +26,7 @@ print(t.sum) --> nil
2726
print(t:sum()) --> 3
2827
```
2928

30-
Beware of a caveat:
31-
```pluto
29+
```pluto title="An example showing __index taking precedence"
3230
local t = { 1, 2 }
3331
print(t:min()) --> 1
3432
t.min = 1

0 commit comments

Comments
 (0)