New Issue: type query for out formals to change the flow of type inference

17198, "mppf", "type query for out formals to change the flow of type inference", "2021-02-19T17:54:33Z"

As @vasslitvinov proposed in when can a different type be passed to an out formal? · Issue #16582 · chapel-lang/chapel · GitHub , we are thinking of allowing one to opt-in to type inference from the call site for out intent formals by providing a type query expression.

Since we find this pattern attractive:

var myVar: MyType;              // not initialized yet
initializeViaOutIntent(myVar);  // split-initialization here

it would be unfortunate if we disallowed the possibility for the out-formal of initializeViaOutIntent() to be generic and get its concrete type from the actual. The user would have to write ex.

initializeViaOutIntent(myVar, myVar.type);

To support the elegant version, I propose to provide syntax for inferring the concrete type of an out-formal from the actual argument. For example, using a query type:

proc initializeViaOutIntent(out arg: ?T) {   // arg.type is determined by the actual
  .....
}

Such a formal would still not participate in candidate selection, as per the leading proposal.

This would enable the scenarios mentioned above:

  • channel.readbits
  • proc foo(out B: []) { for i in B.domain do B(i) = i; }
  • proc f(out arg) { if something then arg = 1; } from SSCA2

PR #16990 adds a compilation error for this case, so changing it should be a non-breaking change.

Note that whenever this is implemented, we will probably want to revert commit 4f825c32682a5cade223f4eff9f8721aec749b24 "Remove special handling for out arguments with runtime types" so that the out arg: ?t could include flow of runtime type information from the call site to the called function (this can be handled in a hidden argument, but that commit removed the support for it from the compiler).