chapel icon indicating copy to clipboard operation
chapel copied to clipboard

[Feature Request]: Add homogeneous value assignment for tuples.

Open Iainmon opened this issue 1 year ago • 2 comments

Summary of Feature

Description:

Wanting to assign a value to every index in a tuple.

var x: 4 * int = 0; // can't do this :(

Is this issue currently blocking your progress?

No, just workarounds.

Solution?

inline operator =(ref tup: _tuple, v: tup(0).type) where isHomogeneousTuple(tup) {
    for param i in 0..<tup.size do
        tup(i) = v;
}

Iainmon avatar Jul 16 '24 14:07 Iainmon

It seems like this method isn't being picked up. Maybe the failure needs to be lifted by the compiler?

inline operator =(ref tup: _tuple, v: tup(0).type) where isHomogeneousTuple(tup) {
    for param i in 0..<tup.size do
        tup(i) = v;
}
var x: 4 * int;
x = 2;
writeln(x);
// ...: error: cannot initialize 'x' of type '4*int(64)' from '2'

Iainmon avatar Jul 16 '24 15:07 Iainmon

It seems like this method isn't being picked up.

@Iainmon : I think that this is because the x=2 is being interpreted as an initialization (e.g., init= routine) rather than an assignment, which is what you've defined here. Note that this is a case where the compiler applies "split initialization" to treat the first assignment as an initialization since there are no intervening references to 'x' or control flow statements that would make it not be treated as such.

Testing this theory by adding an explicit initialization of x seems to confirm this, but runs into a new / different problem ATO:

inline operator =(ref tup: _tuple, v: tup(0).type) where isHomogeneousTuple(tup) {
    for param i in 0..<tup.size do
        tup(i) = v;
}
var x: 4 * int = (0,0,0,0);
x = 2;
writeln(x);
error: unsupported = overload to '4*int(64)' from 'int(64)'
note: assignment overloads setting array or tuples are not currently supported

bradcray avatar Jul 22 '24 18:07 bradcray