Comments (5)
That is another option. It would probably be much easier than introducing new syntax right? I now realize that the only situations where generic table indexers would be actually useful is when you have a generic __index metamethod
from luau.
Could you tell us more about the problem you're trying to solve with this behavior? I'm trying to understand the desire for an indexer with this behavior.
from luau.
It is useful for tables with an __index/__newindex metamethod that is generic. In my case, I am making an entity-component-system library and I'd like to implement an "entity reference", which is basically a proxy table for the actual components in an entity. I'd like to be able to write:
local componentInstance = reference[component]
and make Luau infer the type for the component instance based on the component in the index. This is done easily in a __index function with the following type: <T>(tbl: {any}, key: Component<T>) -> T
, and it works, but when you actually try it...
type Example = typeof(setmetatable({}, {} :: {__index: <T>(tbl: {any}, key: Component<T>) ->T}))
local example = {} :: Example
local str = example[{} :: Component<string>] -- Sadly, this is not inferred as string
So my hope would be that this could solve this issue. I'd be able to simply write:
type Reference = {
<T>[Component<T>]: T
}
Right now, I'm forced to use setters and getters, which not only looks very ugly, but makes it impossible to write nonverbose code.
ref.Set(Health, ref.Get(Health) - 10)
-- vs
ref[Health] -= 10
from luau.
It seems like it would make much more sense for us to make the __index
metamethod typecheck as expected.
from luau.
This looks related to the RFC luau-lang/rfcs#19 which was closed, so I'm gonna close this out. It seems like both solution, __index
and simply changing the API surface would solve your issue.
from luau.
Related Issues (20)
- intersecting class type by shape type results in `never` and causes loss of type inference - new solver bug
- Metamethods with generics having their generics infered as the self type if the self type isn't a generic itself
- First argument of setmetable throws a type error, if the variable is annotated to be a metatable in the new solver
- Setting a key to nil in a table errors in the new solver
- Can't define function in lua script after calling luaL_sandbox in host HOT 2
- `buffer` library doesn't accept `buffer`s in new type solver's non-strict mode HOT 3
- `any` as generic `T` incompatible with `{T}` in new type solver's strict mode
- Variable's type sometimes unions with `nil` due to `if` statement with new type solver's strict mode HOT 1
- For loop variable types not inferred from generic iterator with new type solver
- `and` `or` expression causes type pack error with new type solver's strict mode
- `table.freeze` returns type `{+ +}` with new solver's strict mode HOT 1
- failed assert in script file crashes repl HOT 1
- Stack overflow in parser HOT 3
- `buffer` library `TypeError` when buffer is function parameter with new solver's non-strict mode
- Cyclic union with upvalue not reducing `TypeError` with new solver's strict mode
- TypeError: Type pack '...any' could not be converted into 'a...' in new solver's strict mode
- Comment in lgc.h wrongly describes the GC invariant? HOT 1
- Test doesn't do what it says it does HOT 2
- TypeError when indexing table with union/intersection type of identical types
- Variables pack not inferred correctly for `__iter` metamethod
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from luau.