On Fri, Apr 30, 2021 at 4:23 AM Asher Gordon <asd...@posteo.net> wrote:
> Hello, > > I'm writing a new module that exports a similar interface to Type::Tie, > but performs a type check on the entire variable each time it is > changed. Obviously, this is much more expensive, but it can be useful > for types like Types::Standard::Dict in cases where speed is not very > important. > > In case you're not familiar with Type::Tie, here's a brief summary: > Type::Tie exports a single function, ttie(), which ties variables to > types. For example, after > > use Type::Tie; > use Types::Standard qw(Num); > > ttie my $num, Num; > > setting $num to anything but a number will die. And > > ttie my @num_list, Num; > > only allows numbers in @num_list. So > > push @num_list, "foo"; > > will die. > > Type::Tie::Full (the working name for my module) is similar, except that > the entire variable (as a reference) is passed to the type checker. The > sole exception to this is tying scalars--in this case, the referenced > scalar will be passed to the type checker rather than the reference > itself. > > For example, this works correctly: > > use Type::Tie::Full; > use Types::Standard qw(Dict Optional Str Num); > > ttie my %dict, Dict[string => Str, number => Optional[Num]], > string => 'foo'; > > $dict{string} = 'bar'; # ok > $dict{number} = 42; # ok > $dict{nonexistent} = 'foo'; # dies > $dict{number} = 'foo'; # dies > > delete $dict{string}; # dies ('string' is not optional) > delete $dict{number}; # ok ('number' is optional) > > Currently, only one layer of tying is performed, which means things like > the following don't work how I would like them to: > > use Type::Tie::Full; > use Types::Standard qw(HashRef Num); > > ttie my $hashref, HashRef[Num], {}; > > $hashref = { foo => 'bar' }; # dies ('bar' is not a number) > $hashref->{foo} = 'bar'; # doesn't die, but probably should > > I intend to implement this using deep tying, but it doesn't work for > now. > > My question is, does the name make sense for this module? I figure since > it performs a full check on the variable every time it is changed (as > opposed to a check on just the added elements), then it makes sense to > call it that. Would you agree? > Any namespace under Type::Tie you like seems appropriate to me. One suggestion I have is Type::Tie::Aggregate, since it performs a type check on the aggregate variables rather than the elements. -Dan