Semantics of set() versus Set() versus Set.new

164 Views Asked by At

Should &set, Set, and Set.new have different semantics? If so, why?

Consider the following code:

my @array = 1, 2;
my @other-array = ([3, 4],);

dd set(@array, @other-array);      #OUTPUT: Set.new(1,2,[3, 4])
dd Set(@array, @other-array);      #OUTPUT: Set.new(1,2,[3, 4])
dd Set.new: @array, @other-array;  #OUTPUT: Set.new([1, 2],[[3, 4],])

Is the different output of Set.new intentional or the result of a bug in &set/Set? (I expected all three to have the output produced by Set.new – am I missing something about the intended semantics?)

1

There are 1 best solutions below

4
wamba On

the difference between set and Set.new: is only (Un)flattened slurpy paramters*@a vs. **@a.

my @array = 1, 2;
my @other-array = ([3, 4],);
-> *@a { say @a }(@array, @other-array);
-> **@a { say @a }(@array, @other-array);

Set() is coercion.

my Set() $a = (@array, @other-array);
say $a;
-> Set() $a {say $a}( (@array, @other-array) );
say Set(Set(@array, @other-array)), (@array, @other-array).Set;
say Set(class :: { has (@.array, @.other-array);  method Set () { set @!array, @!other-array } }.new( :@array, :@other-array ));