Skip to content

support for @tupleReturn per overloaded function signature#495

Merged
tomblind merged 2 commits intomasterfrom
tuplereturn-per-signature
Mar 26, 2019
Merged

support for @tupleReturn per overloaded function signature#495
tomblind merged 2 commits intomasterfrom
tuplereturn-per-signature

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

This adds support for per-signature @tupleReturn directives. This allows declaring a function with overloads where some return multiple values, and some do not.

Example:

declare function foo(n: number): number;
/** @tupleReturn */ declare function foo(s: string): [string, string];

const n = foo(1);
const [s1, s2] = foo("bar");

This also works on function types declared as interfaces:

interface Fn {
    (n: number): number;
    /** @tupleReturn */ (s: string): [string, string];
}

Note that if @tupleReturn is placed on the interface itself, it affects all signatures in it:

/** @tupleReturn */
interface Fn {
    (n: number): [number, number];
    (s: string): [string, string];
}

If a function implementation has overloaded declarations, and at least one of those signatures has the @tupleReturn directive, then any array-like return values will be unpacked upon return. Non-array values won't.

Example:

function foo(n: number): number;
/** @tupleReturn */ function foo(s: string): [string, string];
function foo(a: number | string): number | [string, string] {
    if (typeof a === "number") {
        return a;
    } else {
        return [a, "bar"];
    }
}
const x = foo(1);
const [y, z] = foo("bar");
foo = function(self, a)
    if (((type(a) == "table") and "object") or type(a)) == "number" then
        return a;
    else
        return a, "bar"; -- unpacked
    end
end;
local x = foo(_G, 1);
local y, z = foo(_G, "bar"); -- assumed to be unpacked

@tomblind tomblind merged commit 225c7c1 into master Mar 26, 2019
@tomblind tomblind deleted the tuplereturn-per-signature branch March 26, 2019 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants