rfcs/docs/rawget-type-operator
2024-06-05 13:36:51 -07:00

41 lines
1.5 KiB
Text

# `rawget` type operator
## Summary
This RFC proposes the addition on one type operator, `rawget`, which can he used to look up a specific property of another type *without* invoking the `__index` metamethod.
## Motivation
There exists `index` type operator that allow developers to obtain a type of a property from classes / tables. The proposed addition of the rawget type operator aims to provide a way to look up a specific property of a type without invoking the __index metamethod. This can be useful in scenarios where developers need to access properties directly without going through the metamethod. The expected outcome is to enhance flexibility and control when working with types and their properties.
```
-- Create a table
local tbl = {name = "John", age = 30}
-- Set metatable with __index metamethod
setmetatable(tbl, {
__index = function(table, key)
return "Accessed via metatable"
end
})
-- Normal access - this will invoke the __index metamethod
print(tbl.name) -- Outputs: "Accessed via metatable"
-- Using rawget - this will bypass the __index metamethod
print(rawget(tbl, 'name')) -- Outputs: "John"
```
Why are we doing this? What use cases does it support? What is the expected outcome?
## Design
This is the bulk of the proposal. Explain the design in enough detail for somebody familiar with the language to understand, and include examples of how the feature is used.
## Drawbacks
Why should we *not* do this?
## Alternatives
What other designs have been considered? What is the impact of not doing this?