Skip Menu |

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

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

People
Owner: Nobody in particular
Requestors: kfm [...] plushkava.net
Cc:
AdminCc:

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



Subject: Consider documenting other styles of employing Type::Params
After switching to Type::Params, I initially followed the style of declaring $check prior to the line that declares the variables to be populated. Doing so means that the variable names are distanced from the line containing the sub keyword by a minimum of one additional line; potentially more. As such, I found that this increased the cognitive load of discerning the (effective) signature of a sub, at a glance. After some consideration, I came up with the following approach. sub f { my ($str, $num) = do { state $check = compile(Str, Int) }->(@_); } Overall, I think that this is more legible, especially in the case that the validation spec is complex and merits being split over several lines. Later, I realised that the module could peacefully co-exist with the experimental signatures feature. use 5.26.0; # signatures are too slow in earlier versions use experimental 'signatures'; # ... sub f ($str, $num) { do { state $check = compile(Str, Int) }->(@_); } Admittedly, the second approach doesn't mesh with the use of slurpy, although it's still possible to use a plain (@) or (%) signature. However, I think it scores well in terms of legibility and makes default handling more pleasant, even though it is still necessary to use the Optional type. sub f ($str, $num = 0) { do { state $check = compile(Str, Optional[Int]) }->(@_); } While it's not especially important, I was wondering whether anyone else might benefit from the first approach - at least - being mentioned in the documentation.
On 2019-08-21T07:14:17+01:00, kfm@plushkava.net wrote: Show quoted text
> sub f { > my ($str, $num) = do { state $check = compile(Str, Int) }->(@_); > }
This is an interesting idea. I thought, do blocks don't accept parameters! Took me a moment to figure out what was going on… the do block is returning a coderef, and the parameters get passed to that. This might be a bit obscure to promote as an idiom. However, I do wonder if allowing something like this might be a good idea: sub f { my ($str, $num) = compile(\state $check, Str, Int)->(@_); } Within `compile`, if $_[0] is a scalar reference to a coderef, just return it without recompiling. And if it's a scalar reference to undef, set it to a newly compiled coderef. Could be an idea to make it more one-linery.
Subject: Re: [rt.cpan.org #130353] Consider documenting other styles of employing Type::Params
Date: Wed, 21 Aug 2019 16:07:38 +0100
To: bug-Type-Tiny [...] rt.cpan.org
From: "" <kfm [...] plushkava.net>
On Wed, 21 Aug 2019 10:43:10 -0400 "Toby Inkster via RT" <bug-Type-Tiny@rt.cpan.org> wrote: Show quoted text
> However, I do wonder if allowing something like this might be a good idea: > > sub f { > my ($str, $num) = compile(\state $check, Str, Int)->(@_); > } >
Show quoted text
> Within `compile`, if $_[0] is a scalar reference to a coderef, just return it without recompiling. And if it's a scalar reference to undef, set it to a newly compiled coderef.
I'd say that's an excellent idea! It appears about as compact and convenient as it can possibly be while remaining Perlish. Further, it would not interfere with either of the previously related idioms. Show quoted text
> > Could be an idea to make it more one-linery.
Yes, indeed. Besides which, given a more ornate validation spec that may span multiple lines, it really helps to see the variable names up front.
Rejecting. People are capable of figuring out their own favourite idioms.
Subject: Re: [rt.cpan.org #130353] Consider documenting other styles of employing Type::Params
Date: Fri, 6 Dec 2019 17:36:30 +0000
To: bug-Type-Tiny [...] rt.cpan.org
From: "" <kfm [...] plushkava.net>
On Thu, 14 Nov 2019 17:31:48 -0500 "Toby Inkster via RT" <bug-Type-Tiny@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=130353 > > > Rejecting. People are capable of figuring out their own favourite idioms.
Aside from that, is the idea of permitting $_[0] to be a coderef still on the cards?
On 2019-12-06T17:36:39Z, kfm@plushkava.net wrote: Show quoted text
> Aside from that, is the idea of permitting $_[0] to be a coderef still > on the cards?
Not sure. It's pretty easy to do in a wrapper: sub compile { return ${$_[0]} if (ref $_[0] eq 'REF' and ref ${$_[0]} eq 'CODE'); ${+shift} = Type::Params::compile(@_); } It is slower than the currently recommended pattern though because it will always result in an extra sub call. There's a lot more tutorial-style documentation of Type::Params in the current development releases, and I hope they provide a pretty good understanding of what Type::Params is doing (not directly checking your parameters, but compiling a coderef which can). And I think with that understanding, it's pretty easy for people to figure out their own patterns for using it.
Subject: Re: [rt.cpan.org #130353] Consider documenting other styles of employing Type::Params
Date: Sun, 8 Dec 2019 17:58:11 +0000
To: bug-Type-Tiny [...] rt.cpan.org
From: "" <kfm [...] plushkava.net>
On Sun, 8 Dec 2019 12:10:17 -0500 "Toby Inkster via RT" <bug-Type-Tiny@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=130353 > > > On 2019-12-06T17:36:39Z, kfm@plushkava.net wrote:
> > Aside from that, is the idea of permitting $_[0] to be a coderef still > > on the cards?
> > Not sure. It's pretty easy to do in a wrapper: > > sub compile { > return ${$_[0]} if (ref $_[0] eq 'REF' and ref ${$_[0]} eq 'CODE'); > ${+shift} = Type::Params::compile(@_); > } > > It is slower than the currently recommended pattern though because it will always result in an extra sub call.
Exactly. There is no way I would do this. Anyway, I hope you will consider it. Thank you for your time.
I mean that even if the feature were included in the Type::Params's `compile` instead of via a wrapper, it would result in an extra sub call for every time you need to check the parameters, so it's better to use the `state` pattern already documented.