Akuli / jou

Yet another programming language
MIT License
11 stars 4 forks source link

Methods that take self by value #483

Closed Akuli closed 6 months ago

Akuli commented 6 months ago

Sometimes it would be nice to make a method that takes self not as a pointer, but as a value. For example, BigInt in #482 has no methods, and instead there are functions like def bigadd(x: BigInt, y: BigInt) -> BigInt. I did it that way because foo.add(bar).add(baz) didn't work (needs a pointer to foo.add(bar)), but bigadd(bigadd(foo, bar), baz) does work.

Maybe I should do something like this:

class Foo:
    x: int
    y: int

    # Method that takes self a pointer
    def print1(self) -> None:
        printf("%d %d\n", self->x, self->y)

    # Same thing, different syntax
    def print1(self: Foo*) -> None:
        printf("%d %d\n", self->x, self->y)

    # Method that takes self by value
    def print1(self: Foo) -> None:
        printf("%d %d\n", self.x, self.y)

This would also kinda fix #439. You could use . if you want, but it's usually not what you want and usually you do -> instead.