raku

Unexpected flattening of arrays when initalizing an array


I'm not totally sure if this is a bug, a quirk, or simply a bad practice, but this is what happens:

[0] > my @nope = [["a","b"]]; @nope.push: ["b","c"]
[a b [b c]]
[1] > my @yipee = []; @yipee.push: [["a","b"]]; @yipee.push: ["b","c"]
[[a b] [b c]]

So if you inialize an array with a list of lists, it gets Slipped, forcing to use 2 steps to initalize it. Is there something I'm missing here?


Solution

  • Note that for the initialization of arrays, you don't need the outer pair of []. So your first example can be written as:

    my @nope = ["a","b"];
    

    Which may make the issue clearer to you: it's the single argument rule at work (which BTW allows you to say for @a { }). So either you need to make it look like more arguments by adding a comma (which makes it a List):

    my @a = ["a","b"],;
    dd @a;  # [["a", "b"],]
    

    or you need to itemize the first array (as items never get implicitely flattened):

    my @b = $["a","b"];
    dd @b;  # [["a", "b"],]