Comments (3)
@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.
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.
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)
- luau_load can't load this if I export the bytecode and manually put it inside Luau "ASSERTION FAILED: index < count"
- os.date("%Z") returns "Easter" for EDT HOT 1
- Constant array boundary lookups in lua_ref
- Module Declaration for Native Modules HOT 1
- Argument count mismatch on call rather than function HOT 2
- Adding portable prebuilt for aarch64 and x86 in the release HOT 3
- Breakpoint not triggering
- lua_ref failure with too many elements HOT 1
- ~~Byteswap but not bitswap~~
- Autocompleting OO-Style methods is not working in the new type solver. HOT 10
- Inserting into a table with __mode v fails statically
- Autocomplete doesn't work with some intersection/union types
- Type packs are not supported in declaration files HOT 1
- Difference types HOT 4
- Type 'T' could not be converted into 'T' HOT 3
- 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
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.