Subject: | Please add ->check_coerce method which returns undef if coercion fails |
Date: | Thu, 26 Nov 2020 18:10:02 +0100 |
To: | bug-Type-Tiny [...] rt.cpan.org |
From: | BP Jonsson <bpjonsson [...] gmail.com> |
The subject pretty much says it all. There is ->coerce which returns the
original value if coercion fails, and ->assert_coerce which dies if
coercion fails but no middle ground which returns false if coercion fails.
It is true that it would just be sugar for Type->check(
Type->coerce($value) ) but it would be very useful sugar, because frankly
those nested calls are ugly, especially when it's inside an if test which
is commonly the case, and even more so in case you use a coercion object:
Coercion->type_constraint(Coercion->coerce($value))! I'm tempted to use try
{ Foo->assert_coerce($value) } but that is really even more ugly, although
less to type.
I can try to make a pull request if it would be welcome.