Giter Club home page Giter Club logo

Comments (6)

adriweb avatar adriweb commented on September 15, 2024 1

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.

Benjamin-Dobell avatar Benjamin-Dobell commented on September 15, 2024

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.

adriweb avatar adriweb commented on September 15, 2024

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.

Benjamin-Dobell avatar Benjamin-Dobell commented on September 15, 2024

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.

adriweb avatar adriweb commented on September 15, 2024

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.

Benjamin-Dobell avatar Benjamin-Dobell commented on September 15, 2024

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

Screen Shot 2021-01-14 at 12 49 13 am

from intellij-luanalysis.

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.