develooper Front page | perl.perl6.compiler | Postings from July 2018

Re: [perl #131599] [RFC] Allow a Callable for `is default` that willgenerate default values

Thread Previous | Thread Next
From:
Elizabeth Mattijsen via RT
Date:
July 21, 2018 06:57
Subject:
Re: [perl #131599] [RFC] Allow a Callable for `is default` that willgenerate default values
Message ID:
rt-4.0.24-26348-1532156232-1282.131599-80-0@perl.org
FWIW, 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


> On 18 Jul 2018, at 17:32, Alastair Douglas via RT <perl6-bugs-followup@perl.org> wrote:
> 
> On Mon, 19 Jun 2017 09:30:41 -0700, brad wrote:
>> The way Moose in Perl 5 works around this is to give it a subroutine
> 
>> there currently isn't as far as I know, a way to do what you intended.
> 
> 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.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About