I'm trying to implement the cast :
operator between two generic class types in a way that explicitly copies the management strategy from the fromType
.
I know that :
has the signature below (though I forget whether I found that in a doc or inferred it through trial and error).
operator :(from : fromType, type to : toType) {..}
I want to both restrict fromType
to only be instantiations of generic class FooFrom
, but also be able to query the memory management from it. So I'm using a queryable formal, paired with a where clause for the type restriction.
I currently have
class FooFrom {
param commonParam;
... // other common non-param members
}
class FooTo {
param commonParam;
... // other common non-param members
operator :( from : ?fromType, type to: this.type) where isSubtype(fromType, FooFrom(?)) {
assert(to.commonParam == fromType.commonParam, "CAST FAILED");
var tmp = new fromType:class? to(
... // other common non-param members
);
return tmp;
}
}
But I'm getting
<line that new is on>: syntax error: 'new' expression is missing its argument list
<line that new is on>: syntax error: near 'to'
I don't think I'm correctly capturing the memory management of from
, which is what's ultimately breaking the new
expression.
(I came up with the fromType:class?
syntax from here Conversions — Chapel Documentation 1.30, but within a new
expression is clearly not the right place to use it.)
If I remove the attempt to make it explicit, and just do new to(...)
then I actually implicitly get the behavior I want. An unmanaged fromType
becomes an unmanaged FooTo
, a shared
becomes a shared
. But it's a little uncomfortable that this behavior isn't made explicit anywhere I've found (yet).
Am I overcomplicating by trying to control management within the :
operator? And instead should just do any explicit management control in the cast's caller? (Perfectly reasonable if that's the case, or I've missed a doc somewhere which guarantees the copied management semantics.)
Perhaps I just want a param proc
that directly returns the management strategy of a class type
? I know there are boolean ones to check if it is/n't one of the four strategies, but nothing like numBits
that would return the specific strategy, at least not on Types — Chapel Documentation 1.30