A Hash either takes some number of pairs

    my %h = a => 1, b => 2;

or an even number of elements, which become key value pairs

    my %h = 'a', 1, 'b', 2;

`eager` returns an eager Sequence/List etc

   (eager a => 1).raku # (:a(1),)

A Sequence/List is itself a thing. Which means that it can be a key.

So when you pass that to a Hash constructor, it accepts that as the key,
but it still needs the value to go with it.

    my %h{Any} = (eager a => 1), 2;
    # {(a => 1) => 2}

That is the only key in that Hash is itself a Pair object.

    my $key = a => 1;
    my %h = ($key,), 2;
    say %h{$key}; # 2

`eager` only really makes sense on something that is already an Iterable.

    my %h = eager gather { take 'a'; take 1 }

So since `eager` is defined as a Sequence/List operation, it turns its
arguments into that if they aren't already.

    my \value = eager "foo";
    say value.raku; # ("foo",)

What you didn't notice is that your third example also went wrong. It has a
key which is a Pair, and a value which is also a Pair.

    my %h;
    %h{ foo => 1 } = (bar => 2);
    # {foo 1 => bar => 2}

    my %h = ( foo => 1, ),  bar => 2;
    # {foo 1 => bar => 2}

Note that since Hashes have Str keys by default, that the key actually gets
stringified instead of staying a Pair object.

    %h{ "foo\t1" } = bar => 2;

    say ( foo => 1 ).Str.raku; # "foo\t1"

If we use an object key, it doesn't get stringified. Which better shows
what happened.

    my %h{Any} = ( foo => 1, ),  bar => 2;
    # :{(foo => 1,) => bar => 2}


I honestly don't understand why you would `eager` the argument of a `take`.
It's too late to mark the `gather` as eager.

On Tue, Apr 20, 2021 at 12:39 AM Norman Gaywood <ngayw...@une.edu.au> wrote:

> Hi, I can't figure out why the last line here is failing.
> Version 2020.07
>
> $ raku
> To exit type 'exit' or '^D'
> > my %h = gather { take "foo"=>1; take "bar"=>2;}
> {bar => 2, foo => 1}
> > my %h = gather { take "foo"=>1}
> {foo => 1}
> > my %h = gather { take eager "foo"=>1; take "bar"=>2;}
> {foo 1 => bar => 2}
> > my %h = gather { take eager "foo"=>1}
> Odd number of elements found where hash initializer expected:
> Only saw: $(:foo(1),)
>   in block <unit> at <unknown file> line 1
>
> --
> Norman Gaywood, Computer Systems Officer
> School of Science and Technology
> University of New England
> Armidale NSW 2351, Australia
>
> ngayw...@une.edu.au  http://turing.une.edu.au/~ngaywood
> Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062
>
> Please avoid sending me Word or Power Point attachments.
> See http://www.gnu.org/philosophy/no-word-attachments.html
>

Reply via email to