Skip to content

Commit c9c2e76

Browse files
well-in-that-caseSainan
authored andcommitted
Tweak __mindex documentation
1 parent e259def commit c9c2e76

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

docs/New Features/Mindex Metamethod.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff 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.
210

11+
Another example:
312
```pluto
413
local t = setmetatable({}, {
514
__mindex = {
@@ -18,15 +27,10 @@ print(t.sum) --> nil
1827
print(t:sum()) --> 3
1928
```
2029

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:
26-
30+
Beware of a caveat:
2731
```pluto
2832
local t = { 1, 2 }
2933
print(t:min()) --> 1
3034
t.min = 1
3135
print(t:min()) -- attempt to call a number value
32-
```
36+
```

0 commit comments

Comments
 (0)