New Issue: [discussion] Declaring typedefs on classes with memory management strategies

20082, "bmcdonald3", "[discussion] Declaring typedefs on classes with memory management strategies", "2022-06-24T22:43:16Z"


In the code snippet below, a type myType = shared MyClass; has been declared and so all uses of myType will be shared. Since shared objects can also be borrowed, being able to add a borrowed to the myType instance would be valuable for cases when you would like to coerce the type to borrowed, like in the + operator below.

class MyClass {
  var a: int;

type myType = shared MyClass;

inline operator +(l: borrowed myType, r: borrowed myType): myType {
  return new myType(l.a + r.a);

var val1 = new myType(5);
var val2 = new myType(10);

var val3 = val1 + val2;

Here, the code will not compile, because it is expanded to borrowed shared MyClass, but it could make sense for that borrowed to "overload" that shared, since we want to borrow from a shared class. As it works today, the single typedef is not particularly valuable because if you want to change from using shared, you have to write out the full class or cast or something, rather than just specifying in front of the type.


If borrowed were able to be overloaded onto shared class instances, this would make the typedef more useful and avoid needing the second typedef.

What is being proposed here is a slight change to Chapel's conception of borrowed as a memory management strategy, where it could instead be thought of as a memory management strategy modifier on top of owned/shared, rather than only its own thing.


A workaround that has been suggested for the above code is to add a type myBType = myType: borrowed; and that achieves the desired result, but seems unfortunate that you would need those two separate typedefs.