Skip Menu |

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

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

People
Owner: Nobody in particular
Requestors: djerius [...] cpan.org
Cc:
AdminCc:

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



Subject: allow specifing a class name for Type::Params::compile_named_oo
I find myself creating POD classes using (abusing?) Type::Params::compile_named_oo as a constructor.[1] The only thing missing is the ability to name the class so that I can use it as a type constraint in another class. Any chance of adding the ability to explicitly specify a class name for compile_named_oo? Thanks! Diab [1] I could use Moo with isa, etc, but compile_named_oo is a much more concise means of doing so.
On 2020-07-22T15:24:26+01:00, DJERIUS wrote: Show quoted text
> Any chance of adding the ability to explicitly specify a class name > for compile_named_oo?
I think that's beyond the scope of the module. You can do it the other way around; if you've got an existing class: state $check = compile_named( { class => "Existing::Class" }, foo => Int ); If you need a type constraint for the objects returned by $check, I'd suggest: state $check = compile_named_oo( foo => Int ); my $object = $check->( foo => 42 ); my $type = HasMethods['foo']; $type->assert_valid($object);
On Thu Jul 23 04:05:58 2020, TOBYINK wrote: Show quoted text
> On 2020-07-22T15:24:26+01:00, DJERIUS wrote:
> > Any chance of adding the ability to explicitly specify a class name > > for compile_named_oo?
> > I think that's beyond the scope of the module.
Understood. Show quoted text
> You can do it the other way around; if you've got an existing class: > > state $check = compile_named( { class => "Existing::Class" }, foo => > Int ); > > If you need a type constraint for the objects returned by $check, I'd > suggest: > > state $check = compile_named_oo( foo => Int ); > my $object = $check->( foo => 42 ); > > my $type = HasMethods['foo']; > $type->assert_valid($object);
The sweet spot of compile_named_oo is that it generates predicates and getters. To do this with an existing class I'd have to specify attribute information both in the class specification and in the call to compile_named_oo, which goes against my inherent laziness. Thanks, Diab
While I don't like the idea of being able to give a class name in compile_named_oo, I do like the idea of being able to have a type constraint for objects that are produced by it. I'm thinking of something like: class Foo { use Type::Params qw( compile_named_oo ); use Types::Standard qw( Int ); sub get_foo { state $check = compile_named_oo( foo => Int ); my $args = &$check; ...; return $args; } } class Bar { use Moo; use Type::Params qw( ArgsObject ); has foofoo => ( is => 'ro', isa => ArgsObject[ 'Foo::get_foo' ], ); } my $foofoo = Foo::get_foo( foo => 42 ); say $foofoo->foo; # ==> 42 my $bar = Bar->new( foofoo => $foofoo );
Released 1.012000 with an ArgsObject type constraint exportable by Type::Params.
On Wed Oct 28 11:59:50 2020, TOBYINK wrote: Show quoted text
> Released 1.012000 with an ArgsObject type constraint exportable by > Type::Params.
Thanks!