Raku / user-experience

Identifying issues in and improving the Raku user experience
Artistic License 2.0
25 stars 5 forks source link

Destructuring assignment for object #45

Open iacore opened 2 years ago

iacore commented 2 years ago

Currently there is no way to destruct object on assignment.

my (:$key, :$value) = a => 1; # not what you think
# $key is :a(1)
# $value is (Any)

This is weird, since destructing array works both in block signature and in assignment.

jnthn commented 2 years ago

In Raku, destructuring is performed by binding (thus "signature binding"):

my (:$key, :$value) := a => 1;
say $key; # a
say $value; # 1
iacore commented 2 years ago

But how is ths usable?

my ($a, $b) = 1, 2

I don't remember this documented, but := is much better (to catch errors):

my ($a, $b) := 1, 2, 3;

Too many positionals passed to '<unit>'; expected 2 arguments but got 3
  in block <unit> at <unknown file> line 1
jnthn commented 2 years ago

But how is ths usable?

my ($a, $b) = 1, 2

That's list assignment. I suggest looking up some docs on assignment vs binding; Raku offers both, and they are quite different in nature.

iacore commented 2 years ago

The official docs (docs.raku.org) only has example for single-value binding (my $a = $b). Now I understand it better.