Git Product home page Git Product logo

Comments (7)

 avatar commented on May 29, 2024

Made a PR for this: #84

from roblox-lua-promise.

evaera avatar evaera commented on May 29, 2024

It feels like Luau doesn't have the features we need to type Promises correctly yet.

You can either infer types from the constructor, or declare the types in a typedef and have them be completely unchecked. (You also won't get the metatable as part of the type if you do the typedef route as well)

The inferred type of the constructor will contain private fields, and doesn't seem like it can support generics. We ideally want the Promise type to be Promise<T..., E...>

You'd start it off by saying export type Promise<T> = typeof(Promise.new(function end))... and then there's nowhere to put the type variable, because Luau doesn't support passing generic types explicitly

Both of these options seem like non-starters to me. Having private fields and no types for the inner resolved/rejected values in the Promise type because we can't specify generic arguments is not useful.

On the other hand, using a large type definition is separated from the code and is completely unchecked.

Here's a small example of that:

type Promise = {
    andThen: (Promise, (...any) -> Promise | any) -> Promise
}

local Promise = {}
Promise.__index = Promise

-- Type Error: (11,2) Type '{ @metatable Promise, {  } }' could not be converted into 'Promise'
function Promise.new(): Promise
    return setmetatable({}, Promise)
end

function Promise:andThen(func: (...any) -> Promise | any): Promise

end

This doesn't type check. The only option is to cast the return value of Promise.new into the Promise type manually, which is not sound.

I don't think Luau type checking quite has the tools required to type the Promise library yet.

from roblox-lua-promise.

konstantinepapakonstantinou avatar konstantinepapakonstantinou commented on May 29, 2024

I completely agree that hacky solutions are no good here. I don't know if these issues are currently being addressed by the Luau team or not- but it wouldn't hurt to let them know. I don't develop anymore on the platform, but Promises were a really big part of the codebases I worked on and the lack of type checking made things difficult. There's always Roblox-TS, but ideally we wouldn't need that abstraction.

from roblox-lua-promise.

matthargett avatar matthargett commented on May 29, 2024

Yea, you currently run into a few Luau limitations but can still get some pretty good checking for the first level of a Promise chain that has found some cool bugs for me. Here's the way I've made the best out of the current Luau type checking and syntax:

-- TODO Luau: workaround for Luau recursive type used with different parameters error. delete this copy once that feature is added.
export type _Thenable<R> = {
        andThen: <U>(
                self: _Thenable<R>,
                onFulfill: (R) -> () | U,
                onReject: (error: any) -> () | U
        ) -> (),
}

export type Thenable<R> = {
        andThen: <U>(
                self: Thenable<R>,
                onFulfill: (R) -> () | _Thenable<U> | U,
                onReject: (error: any) -> () | _Thenable<U> | U
                -- TODO Luau: need union type packs to parse () | Thenable<U>
        ) -> nil | _Thenable<U>,
}

from roblox-lua-promise.

cxmeel avatar cxmeel commented on May 29, 2024

Looks like Luau supports type packs now: https://luau-lang.org/typecheck#type-packs

from roblox-lua-promise.

ddavness avatar ddavness commented on May 29, 2024

Hi! It's been a while since this issue was opened. Was it considered revisiting whether typing the library in Luau is doable yet?

from roblox-lua-promise.

matthargett avatar matthargett commented on May 29, 2024

It should be possible now with the improvements made to luau in the past year, assuming the new type solver is on by default.

from roblox-lua-promise.

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.