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

Overloading Class Example doesn't pick up correct constructor #609

Open
lewisfogden opened this issue Nov 23, 2024 · 0 comments
Open

Overloading Class Example doesn't pick up correct constructor #609

lewisfogden opened this issue Nov 23, 2024 · 0 comments

Comments

@lewisfogden
Copy link

From testing the second block of code here, but with a more explicit example:

https://docs.exaloop.io/codon/language/classes

class Foo:
    x: int
    y: int

    def __init__(self):                    # constructor
        self.x, self.y = 0, 0
        print('empty', self.x, self.y)

    def __init__(self, x: int, y: int):    # another constructor
        self.x, self.y = x, y
        print('int int', self.x, self.y)

    def __init__(self, x: int, y: float):  # yet another constructor
        self.x, self.y = x, int(y)
        print('int float', self.x, self.y)


Foo()
Foo(1, 2)
Foo(1, 2.3)
a: int = 3
b: int = 5
Foo(a, b)

Output:

> codon run example.py
empty 0 0
int float 1 2       <- should be int int 1 2
int float 1 2
int float 3 5       <- should be int int 3 5

Note that swapping the __init__ order so that the float constructor is first works successfully. (i.e. method definition order matters)

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