Giter Site home page Giter Site logo

Comments (3)

PhoenixWhitefire avatar PhoenixWhitefire commented on June 10, 2024 1

@JohnnyMorganz the issue with that is that I'm "asserting" what the type is and sidestepping the typechecker entirely. Ideally it should be smart enough to figure out that there's no warning to be had instead of me having to force it off, because otherwise there might be a situation where I do forget to define a member because there wasn't a warning.

from luau.

AmaranthineCodices avatar AmaranthineCodices commented on June 10, 2024 1

The way that Luau treats type annotations makes this not workable. A type annotation on a local is a promise that at all points in the program, that local fulfills that type annotation's constraints. This is why Luau is reporting a type error on the initial assignment: because at that point in the program, the local doesn't fulfill the constraints of its annotation. Consider something like this:

type T = { x: string }
local t: T = {} -- no error
if cond() then -- Luau doesn't know if this branch is taken
    print(t.x) -- oh no
end

t.x = "bar"

In order for Luau to prove that this kind of initialization is safe generally, we need to do more robust control flow analysis than we do today, even in the new type solver. We eventually need to do that control flow analysis for other reasons, but it's not something that's likely to come soon. Even when it does arrive, I'm not sure we'll relax this restriction, because of how Luau considers type annotations to be binding promises that hold at all points in the program.

For this kind of initialization, I'd suggest something like this:

type MyFunType = {
    Something: () -> ()
}

local function Something()
    while true do
    end
end

local ObjectThatDoesSomething: MyFunType = {
    Something = Something,
}

Another option would be to construct the table in another local, and then assign to the annotated local once the table is fully constructed:

type MyFunType = {
	Something: () -> ()
}

local obj = {}

function obj.Something()
	while true do
	end
end

local Object: MyFunType = obj

from luau.

JohnnyMorganz avatar JohnnyMorganz commented on June 10, 2024

You can get the behaviour you want using the type assertion operator.
Unsure if that is the recommended way to do this though

type MyFunType = {
    Something: () -> ()
}

local ObjectThatDoesSomething = {} :: MyFunType

function ObjectThatDoesSomething.Something()
    while true do
    end
end

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.