mirror of
https://github.com/luau-lang/rfcs.git
synced 2025-05-04 10:43:48 +01:00
Initial commit for rawget
This commit is contained in:
parent
6002a16fc3
commit
ca4bfbbf95
1 changed files with 41 additions and 0 deletions
41
docs/rawget-type-operator
Normal file
41
docs/rawget-type-operator
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
# `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?
|
Loading…
Add table
Reference in a new issue