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

Use double colon syntax to call static methods #241

Closed
cburgdorf opened this issue Feb 10, 2021 · 0 comments
Closed

Use double colon syntax to call static methods #241

cburgdorf opened this issue Feb 10, 2021 · 0 comments

Comments

@cburgdorf
Copy link
Collaborator

What is wrong?

When #239 lands we'll have support for SomeContract.create(..) and SomeContract.create2(..) which can both be thought as being static methods on a contract type. We should change static call syntax to use double colon syntax instead. To stick to the given examples, they would look like SomeContract::create(..) and SomeContract::create2(..)

Rational:

  1. It clearly separates instance function calls from static function calls
  2. It follows Rust syntax which we prefer to use over Python syntax in situations where we believe Rust syntax brings a clear benefit to the table

This will also work nicely with struct factory methods as demonstrated with the following example taken from #96

struct Rectangle:
    width: u256
    height: u256

# Static method to create a rectangle that happens to be a square    
def square(size: u256) -> Rectangle:
    Rectangle(width=size, height: size)

# An instance method to check if the rectangle can hold another rectangle
def can_hold(self, other: Rectangle) -> bool:
    return self.width > other.width and self.height > other.height

Taking self as the first parameter is what would decide whether something is a static method or an instance method (just like in Rust)

Usage:

big = Rectangle(width=10, height=20)
square = Rectangle::square(5)
big.can_hold(square)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant