Comments (7)
This is a little tricky. It allows declaring functions over unknown keys of records to support this:
local my_module = {}
function my_module.my_function()
-- ...
end
...which is a super common pattern.
What you might be suggesting is that function t.unknown_key
should work differently whether the table is declared in the same file or required from a different one (or perhaps it should be a matter of scope levels instead). In any case, it's an intriguing idea.
from tl.
Perhaps there should be an alternative record declaration syntax that forces you to declare all fields upfront? Something like this:
declare global love = record -- I'm not a fan of this 'declare' keyword... maybe you have a better idea?
draw: function()
end
function love.draw() -- this works, since the `draw` key is known
print("draw stuff...")
end
function love.draws() -- error: unknown key 'draws' in 'love'
print("draw stuff...")
end
I think this feature would be useful for declaration files, since I don't think we typically want to "extend" the definitions outside of the initial record
declaration.
from tl.
@pdesaulniers I made it in #48 so that it decides by scope whether it's ok or not to create a new function, the PR contains a description (and so do the tests), check it out!
from tl.
OK, that works!
I guess this behavior might seem a bit surprising for newcomers. Personally, I would expect global records to behave the same way as local records.
But then, I think it's fine for now. Global variables should generally be avoided anyway :)
from tl.
I agree, but then either love.draws
would have to be accepted, or people would be forced to pre-declare every function in their module. At least I think I managed to produce a reasonable error message that explains the situation. At one point I'd like to do what the Rust compiler does and put "pointers" in the error messages such as error code numbers, that people can easily search online and reach an explanation that's longer than one line.
from tl.
Yep. This could be mentioned in the docs.
I think this behavior is fine for the time being.
from tl.
Yeah, will definitely document it... when I get around to start the documentation! Which should probably be the next thing for me to work on!
Going to merge the PR and close this one, thanks for bouncing ideas :)
from tl.
Related Issues (20)
- No errors reported when assigning value directly to a record table
- Function does not enforce argument record type if variable type is inferred
- Type alias for nested generic crashes compiler HOT 1
- [next] `self` cannot be constrained by an interface
- [next] interface methods
- Possible Typechecking Failure on Union Return HOT 2
- unexpected propagation of <const> attribute HOT 1
- missing narrowing type when not simple expression
- Inconsistencies between string escapes in Lua versions are not handled by Teal
- Feature request: Ability to rename the compiled files using regex HOT 2
- weird shadowing behavior regarding generic type names HOT 2
- reexport a nested table type HOT 1
- How do i solve this error? : argument 2: got type constant (alias), expected constant HOT 2
- flow-typing of return types at odds with polymorphic functions
- Support forward declarations, at least in a record HOT 3
- Nested record metatable issue? HOT 1
- [next] declaration of record methods does not work for aliased types HOT 2
- [next] union type does not specialize with and-or idiom
- [next] type alias definition gets confused with generics
- [next] Internal Compiler Error on `tl types` HOT 1
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 tl.