Giter Site home page Giter Site logo

Comments (5)

mathf-x avatar mathf-x commented on June 10, 2024 1

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.

AmaranthineCodices avatar AmaranthineCodices commented on June 10, 2024

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.

mathf-x avatar mathf-x commented on June 10, 2024

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.

andyfriesen avatar andyfriesen commented on June 10, 2024

It seems like it would make much more sense for us to make the __index metamethod typecheck as expected.

from luau.

alexmccord avatar alexmccord commented on June 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.