Skip to content

[BUG] friend and this for commutative operators #576

@JohelEGP

Description

@JohelEGP

Title: friend and this for commutative operators.

Description:

This is a first write of an operator* in Cpp2:

quantity: @struct <Unit: type> type = {
  number: i32;
  operator*: (this, r: i32) -> quantity = (number * r);
}
operator*: <Unit> (l: i32, r: quantity<Unit>) -> quantity<Unit> = r * l;

The signatures are unfortunately too dissimilar and far apart.
We could remediate:

quantity: @struct <Unit: type> type = {
  number: i32;
  operator/: /* ... */;
  operator%: /* ... */;
}
operator*: <Unit> (l: quantity<Unit>, r: i32) -> quantity<Unit> = (number * r);
operator*: <Unit> (l: i32, r: quantity<Unit>) -> quantity<Unit> = r * l;

Not only is this more verbose,
we've moved them away from similar operators,
operators which still share the dissimilarity.
I think this is the ideal:

quantity: @struct <Unit: type> type = {
  number: i32;
  friend operator*: (this, r: i32) -> quantity = (number * r);
  friend operator*: (l: i32, this) -> quantity = r * l;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions