New Issue: How should operator methods interact with forwarding?

16992, “lydia-duncan”, “How should operator methods interact with forwarding?”, “2021-01-22T23:32:29Z”

Brad brought up on the operator keyword issue that we should think about how operators as methods should interact with forwarding.

record Type1 {
  var x: int;

  proc type +(lhs: Type1, rhs: Type2) { ... }
}

record Type2 {
  forwarding var a: Type1;
  
}

I can see a couple of stances here.

One stance is that if the forwarded type is so important to the behavior of the type that contains it, it’s probably reasonable to assume that the operators defined on the forwarded type would be useful and so should be included.

The other is that it could be surprising to discover that suddenly your type is able to be added or multiplied or compared if you weren’t expecting it, when you were focusing on a specific other aspect of the forwarded type’s functionality. Especially when the forwarded field is generic - suddenly sometimes your type can get added and sometimes it can’t.

Another thing to note is that operators (and type methods in general) can have different behaviors depending on the operator - > and <, e.g., are expected to return a boolean so can reasonably have their behavior used, but + and - are frequently expected to return a new instance of the types being combined. Having the addition operation on instances of Type2 in the example above return a Type1 would probably be surprising for the person trying to add two instances of Type2.

A savvy user can write an explicit version of the operator for Type2 to avoid this problem. They could also limit the forwarded methods from the type via an only list (though since we don’t allow operators to be named in limitation clauses, they couldn’t write except + today, for instance).

Sidebar:
This is a more motivated argument for allowing operators in an only or except list - in the case of use and import statements we’re more likely to care on a type granularity rather than an operator granularity, but for forwarding that isn’t a concern since we’re already limiting it to a particular type.