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

How to handle overloads #7

Open
simonseyock opened this issue Sep 2, 2023 · 0 comments
Open

How to handle overloads #7

simonseyock opened this issue Sep 2, 2023 · 0 comments

Comments

@simonseyock
Copy link
Member

simonseyock commented Sep 2, 2023

currently I implemented this concept:

class A {
    private double distanceToConstraints(Point p) {
        return 1;
    }

    private double distanceToConstraints(double x, double y) {
        return 2;
    }
}
export class A {
    private distanceToConstraints(p: Point): number;
    private distanceToConstraints(x: number, y: number): number;
    private distanceToConstraints(...args: any[]): number {
        if (args.length === 1 && args[0] instanceof Point) {
            let p: Point = args[0];
            return 1;
        }
        if (args.length === 2 && typeof args[0] === "number" && typeof args[1] === "number") {
            let x: number = args[0];
            let y: number = args[1];
            return 2;
        }
        throw new Error("overload does not exist");
    }
}

I think it should work for now. I only worry about the typeof .. === "number" etc. checks. These might be to restrictive. (following: "Be liberal in inputs and conservative in your outputs").

Loosening the input restrictions could lead to not be able to identify the input signatures correctly anymore.

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

No branches or pull requests

1 participant