Skip Menu |

This queue is for tickets about the Type-Tiny CPAN distribution.

Report information
The Basics
Id: 89696
Status: resolved
Priority: 0/
Queue: Type-Tiny

People
Owner: perl [...] toby.ink
Requestors: bpj [...] melroch.se
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Types::Standard: is it possible to check for an empty ArrayRef/HashRef?
Date: Tue, 22 Oct 2013 14:22:19 +0200
To: bug-Type-Tiny [...] rt.cpan.org
From: BPJ <bpj [...] melroch.se>
Is it possible to check for an empty ArrayRef or HashRef with Types::Standard? The problem is I want to make a constraint which accepts either an empty ref or a ref with some specific (type of) contents. It seems now I can only accept either a ref with some specific content (Tuple/Map), a ref with some specific type of content (ArrayRef/HashRef with params) or a ref with any content (ArrayRef/HashRef without params). To be sure I can do something like my $empty_hash = declare 'EmptyHashRef', as HashRef, where { 0 == keys %$_ }; my $foo = union [ $empty_hash, Map[ Str, Str ] ]; but I see myself doing this all the time, so it would be nice if EmptyArrayRef and EmptyHashRef or something with the same effect were bundled in. /bpj
union[$empty_hash,Map[Str,Str]] would be equivalent to Map[Str,Str] anyway, because the empty hash passes Map[Str,Str]. I think perhaps Dict[] (with empty brackets) probably should be an explicit check for an empty hashref; and Tuple[] should correspondingly check for an empty arrayref. But they do not currently work that way - I think that's arguably a bug.
Subject: Re: [rt.cpan.org #89696] Types::Standard: is it possible to check for an empty ArrayRef/HashRef?
Date: Wed, 23 Oct 2013 09:46:36 +0200
To: bug-Type-Tiny [...] rt.cpan.org
From: BPJ <bpj [...] melroch.se>
2013-10-23 02:17, Toby Inkster via RT skrev: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=89696 > > > union[$empty_hash,Map[Str,Str]] would be equivalent to > Map[Str,Str] anyway, because the empty hash passes > Map[Str,Str].
It does? So Map[Str,Str] means "*If* the hashref has any keys/values they should be strings" rather than "the hashref should not be empty and its keys/values should be strings"; in that case I think there ought to be some way to check for the *non*-emptiness of a hash/array ref. Perhaps an optional additional parameter (Boolean)? Show quoted text
> I think perhaps Dict[] (with empty brackets) probably should be > an explicit check for an empty hashref; and Tuple[] should > correspondingly check for an empty arrayref. But they do not > currently work that way - I think that's arguably a bug.
Actually that was how I expected them -- and Map[] -- to work! /bpj
I've written a fix for Dict[] and Tuple[] which are not explicit type constraints for empty hashrefs and arrayrefs respectively. This is in the repo, and will be in the next stable release. If you want an explicit non-empty hash or array you can combine various type constraints using overloaded operators: ( ArrayRef[Int] ) & ~ ( Tuple[] ) would be an ArrayRef of Ints and cannot be empty. Or, if the overloading is a little too magic-looking, you could use the object-oriented style to make it more explicit: Type::Tiny::Intersection->new( type_constraints => [ ArrayRef->parameterize(Int), Tuple->parameterize()->complementary_type, ], ) I'm marking this ticket as "patched" for now; once this change gets into a stable release I'll mark it as "resolved". I probably still ought to write a bit more documentation in this area.
Tuple[] and Dict[] work in 0.032.