Request: Create `NonEmpty` type that checks passed type for emptiness.
mvsjes2 opened this issue · 2 comments
Similar to Maybe and Optional, create a wrapper type called NonEmpty
to wrap Str, Array, and Hash types, deprecate NonEmptyStr and NonEmptySimpleStr. Examples:
NonEmpty[Str]
NonEmpty[SimpleStr]
NonEmpty[ArrayRef]
NonEmpty[HashRef]
This is just for convenience and readability.
Is NonEmpty[Str]
really more readable and convenient than NonEmptyStr
? It's two extra punctuation characters to type.
When it comes to method calls, the parameterized version would be worse still. Consider:
if ( NonEmptyStr->check( $foo ) ) {
say "Foo isn't empty!";
}
Versus:
# Doesn't work because method call on an arrayref...
if ( NonEmpty[Str]->check( $foo ) ) {
say "Foo isn't empty!";
}
# Ugly but works...
if ( (NonEmpty[Str])->check( $foo ) ) {
say "Foo isn't empty!";
}
# Prettier but longer...
if ( NonEmpty->of( Str )->check( $foo ) ) {
say "Foo isn't empty!";
}
Most of your suggestions are already fairly concise in existing versions of Type::Tiny. It's only the hashref which gets more complicated.
NonEmpty[Str] # NonEmptyStr
NonEmpty[SimpleStr] # NonEmptySimpleStr
NonEmpty[ArrayRef] # ArrayRef[Any, 1]
NonEmpty[HashRef] # HashRef->where( 'keys(%$_)' )
# Another one...
NonEmpty[ArrayRef[Int]] # ArrayRef[Int, 1]
But in most cases people needed a non-empty hashref, the Dict
type is probably what they want — it allows them to require specific keys in the hashref.
I'm not saying 'no' entirely, but I'm not convinced it would be easier, more concise, or more useful for anybody. I'm not going to include it in Types::Standard or Types::Common::* unless there's some really good reason to. But there's nothing stopping you from publishing a type library of your own including it.