Comments (6)
Interestingly, TypeScript 4.2 just got the feature to have variadic/rest args in other positions than the end: https://devblogs.microsoft.com/typescript/announcing-typescript-4-2-beta/#leading-middle-rest-elements-in-tuple-types 😁
from intellij-luanalysis.
I was wondering if it would be possible to support annotating functions taking n variadic args then a known fixed type
As far as type systems go, any such type would be inherently unsound because it's impossible to know how many arguments are required to call such a function.
It sounds more like you're after some meta-type functionality which would allow you to derive new types from other types. In this case, specifically altering a parameter list.
You can see some rather creative solutions people have come up with for TypeScript:
https://stackoverflow.com/questions/60323726/typescript-add-one-argument-to-a-functions-params
Whilst this certainly is not impossible to implement, unfortunately, it's honestly far more sophisticated than anything I'm planning.
If you've any control over the API in question I'd be inclined to strongly suggesting avoiding writing an API that appends parameters to a parameter list. It seems unnecessarily complicated / error prone, particularly when optional parameters / overloads get involved. Prepending parameters would be much more manageable.
from intellij-luanalysis.
Alright, thanks for the detailed reply - I don't actually disagree with you, but had a bit of hope just in case :D
And yeah unfortunately it's an external lib, as you can see for yourself: https://github.com/luvit/luvit/blob/master/deps/fs.lua (the async functions)
from intellij-luanalysis.
Was just thinking about this some more. No intent to implement it, however it's probably not quite as unsound as I claimed. At least, it's not so much an issue for callers as it is for implementers. Basically the inferred parameter types aren't very useful:
---@alias WeirdCallback fun(a: string, ...: T, c: string): void
---@type fun(weirdCallback: WeirdCallback): void
local callWeirdCallback
callWeirdCallback(function(a, b, c) -- INFERRED fun(a: string, b: string | T, c: nil | string | T): void
end)
Of course you probably wouldn't provide a callback with explicit parameters, but instead you'd use ...
. However, use of ...
isn't type-safe unless you've got an absurd amount of functionality.
Even if the IDE was to infer ...
as the type (string, ...T, string)
you can't actually access that last string by any simple means that an IDE is going to understand because T may include nil
and thus you need to work with table.pack
rather than the table length operator.
Anyway, I just thought this was interesting. It's not impossible to implement, just impractical 😛
from intellij-luanalysis.
Right, what they do in TS is quite complex indeed :P
Basically, we'd need to "forward" type definition/safety from a signature to another... at an arbitrary location (before other args, in this case). It's definitely something a bit exotic and too complex for now I guess.
from intellij-luanalysis.
On the plus side, I totally beat them to the smarter type alias preservation feature 😆
---@alias BasicPrimitive number | string | boolean
---@param value BasicPrimitive
function doStuff(value)
if (math.random() < 0.5) then
return nil
end
return value
end
from intellij-luanalysis.
Related Issues (20)
- nil checks are not context-aware
- Hello, Is it possible to type a global value with Luanalysis ?
- Top and Bottom Types Distinct from "any" HOT 5
- Unnecessary '0 results are excess arguments' warning HOT 1
- Invalid type mismatch error is given when passing along optional `shape` from one function to another HOT 3
- Type mismatch error when assigning subclasses to an array of superclass HOT 2
- java.lang.NullPointerException HOT 2
- `table<>` definitions with incorrect Type Mismatch errors HOT 1
- Add std defs for LuaJIT
- Allow custom function definitions in table
- Support multi-line text in @field and @param comments
- There seems to be no way to define a non-nil generic return value for a function
- Wrong return type for os.execute
- error HOT 1
- Returned anonymous function is not type-checked HOT 1
- Luanalysis doesn't seem to understand manual nil check
- Executing the Rename action on some files causes severe freeze
- Lua code formatting moves some end-of-line comments to a new line
- Upgrade EmmyLua version. HOT 1
- Add support for multiline alias
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 intellij-luanalysis.