Skip Menu |

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

Report information
The Basics
Id: 128493
Status: rejected
Priority: 0/
Queue: Type-Tiny

People
Owner: Nobody in particular
Requestors: violapiratejunky [...] gmail.com
Cc:
AdminCc:

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



Subject: Char type in Types::Common::String
I was wondering if you thought a 'char' type would make sense in Types::Common::String? Something along the lines of: declare Char, as StrLength[1, 1], message { 'Char must be a Str of length 1' };
On 2019-02-13T04:49:06Z, srchulo wrote: Show quoted text
> declare Char, as StrLength[1, 1], > message { 'Char must be a Str of length 1' };
What are potential use cases when you might want to accept an arbitrary string of length 1? I can imagine cases where you might want to accept, say, a single letter or a single byte. But accepting any Unicode character sounds like something that would be needed pretty rarely. The way you define it, Char would accept "a" or "A" or "1" or "ß" or "個" or the crying face emoji.
On Mon Feb 18 12:37:09 2019, TOBYINK wrote: Show quoted text
> On 2019-02-13T04:49:06Z, srchulo wrote:
> > declare Char, as StrLength[1, 1], > > message { 'Char must be a Str of length 1' };
> > What are potential use cases when you might want to accept an > arbitrary string of length 1? I can imagine cases where you might want > to accept, say, a single letter or a single byte. But accepting any > Unicode character sounds like something that would be needed pretty > rarely. > > The way you define it, Char would accept "a" or "A" or "1" or "ß" or > "個" or the crying face emoji.
I'm making updates to my Short::URL module: https://metacpan.org/pod/Short::URL This allows you to map an ID to a string over a given alphabet, but each member of the alphabet should only be one character long, or it would break the algorithm. The examples you provided above would all be fine in this case. If you feel that this is a rare use case, I can just define it locally within my module.
I think it's probably not very common. Note that in the current development version you can do this: use Types::Standard StrMatch => { of => qr/\A.\z/, -as => "Char" }; Char->assert_valid('a'); # returns 'a' Char->assert_valid('ab'); # dies The type checking code that this will generate is pretty efficient. You can see the exact Perl code it will use to check a value this way: warn Char->inline_check( q{$value} );
On Mon Feb 18 14:36:24 2019, TOBYINK wrote: Show quoted text
> I think it's probably not very common. > > Note that in the current development version you can do this: > > use Types::Standard StrMatch => { of => qr/\A.\z/, -as => "Char" }; > > Char->assert_valid('a'); # returns 'a' > Char->assert_valid('ab'); # dies > > The type checking code that this will generate is pretty efficient. > You can see the exact Perl code it will use to check a value this way: > > warn Char->inline_check( q{$value} );
Cool! Thank you. That's helpful :) Is that a better alternative to StrLength[1, 1]?
StrMatch[qr/\A.\z/] is better optimized than StrLength[1,1] $ perl -MTypes::Standard=-all -E'say StrMatch->of(qr/\A.\z/)->inline_check(q/$value/)' (!ref($value) and length($value)==1) $ perl -MTypes::Common::String=-all -E'say StrLength->of(1,1)->inline_check(q/$value/)' (((Type::Tiny::XS::Str($value))) && (length($value) >= 1) && (length($value) <= 1))
On Mon Feb 18 18:38:20 2019, TOBYINK wrote: Show quoted text
> StrMatch[qr/\A.\z/] is better optimized than StrLength[1,1] > > > $ perl -MTypes::Standard=-all -E'say StrMatch->of(qr/\A.\z/)-
> >inline_check(q/$value/)'
> > (!ref($value) and length($value)==1) > > > $ perl -MTypes::Common::String=-all -E'say StrLength->of(1,1)-
> >inline_check(q/$value/)'
> > (((Type::Tiny::XS::Str($value))) && (length($value) >= 1) && > (length($value) <= 1))
Ah, very cool. Thanks!