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

classes #2

Closed
somucheffort opened this issue Jan 20, 2023 · 1 comment
Closed

classes #2

somucheffort opened this issue Jan 20, 2023 · 1 comment
Labels
enhancement New feature or request interpreter Improvements to interpreter

Comments

@somucheffort
Copy link
Owner

somucheffort commented Jan 20, 2023

class Rectangle {
    _(height = 1, width = 1)

    area() -> this.height * this.width
}

class Square : Rectangle {
     _(a = 1) {
          super(a, a)
     }

     toRect(mul = 1) -> new Rectangle(this.height, this.width * mul)
}

let rect = new Rectangle(2, 5)

log(rect) // Rectangle { height: 2, width: 5 }
log(rect.area()) // 10
// this will throw an error
// log(rect.toRect(2)) // unknown method

let square = new Square(4)

log(square) // Square { height: 4, width: 4 }
log(square.area()) // 16
log(square.toRect(2)) // Rectangle { height: 4, width: 8 }

Rectangle.prototype.toSquare = (fromHeight = true) -> {
     if (fromHeight) {
          return new Square(this.height)
     }

     return new Square(this.width)
}

log(rect.toSquare()) // Square { height: 2, width: 2 }
@somucheffort somucheffort added the enhancement New feature or request label Jan 20, 2023
@somucheffort somucheffort added the interpreter Improvements to interpreter label May 19, 2023
@somucheffort
Copy link
Owner Author

closed. see #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request interpreter Improvements to interpreter
Projects
None yet
Development

No branches or pull requests

1 participant