New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a Callable for is default
that will generate default values
#6350
Comments
From @mscha#!/usr/bin/env perl6 my %sum{Int} is default([]); say (1..10).grep(-> $i { %sum{$i} == 2 }); %sum{3}.push: "1+2"; say (1..10).grep(-> $i { %sum{$i} == 2 }); say %sum; say %sum{4}; # Without "is default([])" it works fine, except that the "grep" complains # % perl6 --version |
From @b2gillsThere does appear to be a bug, but I'd argue that it is in your code. my %sum{Int} is default([]); That line of code sets the default for all elements when they are first accessed Remove the `is default([])` To stop the warnings that would then happen you could use `andthen` say (1..10).grep(-> $i { %sum{$i} andthen $_ == 2 }); Or you could use `.elems` say (1..10).grep(-> $i { %sum{$i}.elems == 2 }); The way Moose in Perl 5 works around this is to give it a subroutine that will Basically Perl 6 dutifully did what you asked it to, and there currently So this isn't really a bug report, but a feature request. On Sun, Jun 18, 2017 at 4:52 PM, Michael Schaap
|
The RT System itself - Status changed from 'new' to 'open' |
From @mschaThanks for explaining, I see why it does what it does now, more or But if not a bug, I'd say it's at least an LTA; Rakudo should warn that On 19-Jun-17 18:30, Brad Gilbert via RT wrote:
|
From @zoffixznetOn Mon, 19 Jun 2017 09:30:41 -0700, brad wrote:
PS: note that, as a general usecase, you also need a `:v` there. Otherewise you're .elems'ing an Any, which follows the anything-is-a-1-item-list rule and you end up with .elems being 1 for keys without values: my %h; say %h<meows>.elems |
From alastair.douglas@gmail.comOn Mon, 19 Jun 2017 09:30:41 -0700, brad wrote:
I'd like this feature as well. I was in IRC asking about whether we could restrict a hash in the same way python does, such that %hash<missing-value> dies. It was noted that one can do my %h is default(Failure.new); This would put a Failure in anything that didn't exist, which would detonate whenever accessed. Presumably, this would be the same Failure each time, but that's probably OK. It means there is no way of generating a default based on access. I think that would look something like: my %h is default(-> $key { Failure.new("$key not provided") }); But then how would it know to run the Callable to generate the default, rather than simply providing the Callable as the default? I have no answer for that. |
From @lizmatFWIW, with: http://modules.perl6.org/dist/Hash​::Restricted​:cpan​:ELIZABETH one can restrict access to a hash to a certain set of keys: use Hash::Restricted; my %h is restricted = a => 42, b => 666; # restrict to keys at initialization my %h is restricted<a b c>; # restrict to keys a, b, c
|
Migrated from rt.perl.org#131599 (status was 'open')
Searchable as RT131599$
The text was updated successfully, but these errors were encountered: