Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ... of a variadic function in "normal" function #873

Closed
atticus-sullivan opened this issue Dec 11, 2024 · 2 comments
Closed

Use ... of a variadic function in "normal" function #873

atticus-sullivan opened this issue Dec 11, 2024 · 2 comments
Labels
semantics Unexpected or unsound behaviors

Comments

@atticus-sullivan
Copy link

atticus-sullivan commented Dec 11, 2024

Use-case: Writing a variadic function wich uses a binary function to reduce its arguments.

Example:

local function foo(a:string, b:string): string
    print(a,b)
end

local function bar(...: string): string
    return foo(...)
end

(playground currently not showing the error until I do a change like removing and adding one parenthesis for example).

I'm not sure if this always should compile as ... might be more/less than (in this case) two values, but commonly it is guarded (via select('#', ...)) that the argument count is >= (and usually in lua the additional arguments are just discarded).

As I think about this while writing this, maybe this is not quite a bug report but more seeking for guidance how to write something like this in current teal (just upgraded from 0.15.3 where the above was still working) and maybe a feature request (still not sure how to make this work in a type-safe manner though).

For now the only alternative I'm aware of is putting ... in a table (this is not safe in case nil is involved though) and then access as an array.

@hishamhm
Copy link
Member

There are some alternatives.

If you know that bar might be called with fewer than 2 arguments but your foo is fine with it, then you may want to specify that the arguments in foo are optional, and that will compile:

local function foo(a?: string, b?: string): string
    print(a,b)
end

local function bar(...: string): string
    return foo(...)
end

If you know that bar will always be called with at least 2 entries (assuming you also use ... for other things in your code), then you may want to unpack the first two entries of ... in the signature:

local function foo(a: string, b: string): string
    print(a,b)
end

local function bar(a: string, b: string, ...: string): string
    return foo(a, b)
end

If you want a more 0.15-like behavior, this still works:

local function foo(a: string, b: string): string
    print(a,b)
end

local function bar(...: string): string
    local a, b = ...
    return foo(a, b)
end

@hishamhm hishamhm added the semantics Unexpected or unsound behaviors label Dec 18, 2024
atticus-sullivan added a commit to atticus-sullivan/cluttealtex that referenced this issue Dec 18, 2024
@atticus-sullivan
Copy link
Author

atticus-sullivan commented Dec 18, 2024

Oh I really like your second solution. Combined with using ?string and checking for nil in order to test if only one or two arguments were given.

Thanks for the suggestions 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

2 participants