Problem:
Invoking clear on an associative array incorrectly results in the following error:
error: clear() is only supported on dense 1D arrays
However it is possible to invoke clear on an associative domain. I believe this is an oversight in design or was overlooked when associative arrays were implemented. Correct me if I am wrong, but this should be changed to allow invocation on any domain that supports it and should not check specifically for 'dense 1D arrays'.
@LouisJenkinsCS: I think this was intentional, but that's not to say that it couldn't be changed. Could you state what you want clear() on an associative array to do? (I can think of at least a few interpretations and am not sure which you'd expect). Thanks.
I'd expect clear on an array to invoke clear on it's domain, as in clear all key-value pairs. The only other interpretation I can think of is to clear all values from the associative array, but I would expect a vector assignment operation to handle that... I.E:
var dom : domain(string);
var arr : [dom] string;
// Fill + Print
for i in 1..10 {
dom += ("Hello" + i);
arr["Hello" + i] = ("World" + i);
}
writeln("Array Pre-Vector-Assignment: ", arr);
// If user wants to clear all values
arr = "";
writeln("Array Post-Vector-Assignment: ", arr);
writeln("Array.size=", arr.size);
// If user wants to clear all keys + values
// arr.clear() == dom.clear()
dom.clear();
writeln("dom.clear() => Array.size=", arr.size);
OK, that was my preferred interpretation as well. I'd like @benharsh to comment on this since he implemented a lot of the original "associative array as set" features. I think to be consistent with our current features, we should retain the chpl__assertSingleArrayDomain("clear"); logic that currently exists for the 1D rectangular array-as-vector case (so that clearing an array that shares its domain with another array is not permitted).
I'll note that several of us at times (@benharsh , @mppf, @nspark) have expressed regret for implementing these array-as-vec, associative-array-as-set concepts on arrays directly rather than on objects that wrapped / forwarded to arrays and that I think we still may want to move away from such features in the longer-term. But I don't have a problem with adding this clear() operation to associative arrays for the time being while we're still in this world to round out the current set of features if there isn't a reason we skipped it in the original design that I'm forgetting (@benharsh?).
I don't think there's a reason we skipped it in the original design. I think the 'associative set' stuff predated array-as-vec, so it probably didn't come up.
I'd be OK with adding that functionality given the state we're already in.
Most helpful comment
I don't think there's a reason we skipped it in the original design. I think the 'associative set' stuff predated array-as-vec, so it probably didn't come up.
I'd be OK with adding that functionality given the state we're already in.