Is there a simple way to use the value of a defined constant as a hash / pair key in Perl6?
For instance :
constant KEY = "a string";
my %h = ( KEY => "a value" );
This will creatre a key of "KEY" not "a string".
I can do :
my %h = ( "{KEY}" => "a value" );
But that seems a bit clunky. I was wondering if there was a better way?
The most convenient options would be to either:
constant $KEY = "a string";
), thus avoiding the problem in the first place(KEY) => "a value"
), so it won't be treated as a literalpair(KEY, "a value")
Also, note that:
my %h = ( "{KEY}" => "a value" );
Is a useless use of parentheses, and that:
my %h = KEY, "a value";
Will also work, since non-Pair
s in the list of values to assign to the hash will be paired up. It loses the visual pairing, however, so one of the previously suggested options is perhaps better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With