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

Add support for operator overloading #56

Open
mcfriend99 opened this issue Sep 1, 2021 · 3 comments
Open

Add support for operator overloading #56

mcfriend99 opened this issue Sep 1, 2021 · 3 comments
Labels
Proposal Proposed feature

Comments

@mcfriend99
Copy link
Member

Is your feature request related to a problem? Please describe.

As it stands, Blade classes are first class and can be used in almost anything including iterables, values and more. It will greatly increase consistency to have Blade classes support operator overloading from classes.

Proposed Solution

Since today, Blade classes can become iterators by implementing the @iter and @itern decorators, Blade can employ decorators for operator overloading support.

E.g.

class A {
    @+(a2) {
        return self.value + a2.value
    }
}

var a1 = A()
a1.value = 5

var a2 = A()
a2.value = 7

echo a1 + a2

The above sample code should print 12 to terminal.

Alternative Solution

As an alternative, we could use a keyword operator.

For example:

class A {
    operator + {
        return self.value + __arg__.value
    }
}

var a1 = A()
a1.value = 5

var a2 = A()
a2.value = 7

echo a1 + a2

This will at best require an extra conditionally valid keyword __arg__.

@mcfriend99 mcfriend99 added the Proposal Proposed feature label Sep 1, 2021
@BenStigsen
Copy link
Contributor

BenStigsen commented Sep 15, 2021

I'm mostly for the operator keyword, but I'm not too familiar with the purpose of the @ character, which may or may not change my opinion.

As for the __arg__ keyword. Wren does this very nicely with an other keyword. So this could be:

class Point {
  Point(x, y) {
    self.x = x
    self.y = y
  }

  operator - {
    return Point(self.x - other.x, self.y - other.y)
  }
}

It feels very natural.
It might also be important to keep negating values in mind (if this is to be implemented). So with your suggested syntax above, how would I do:

var p = Point(5, 2)
print(-p) /* should this work? */

Wren fixes this by having:

- {
  // negating
}

- (other) {
  // subtraction
}

@mcfriend99
Copy link
Member Author

The @ character is used for creating decorators

@BenStigsen
Copy link
Contributor

Hmm, to me @-(other) {} does not seem to align with decorators. I think that introducing the keyword operator might be a good way to clarify different types of functionality.

@mcfriend99 mcfriend99 added this to To do in Feature Development via automation Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal Proposed feature
Projects
No open projects
Development

No branches or pull requests

2 participants