Re: Possible to use coerce and auto_deref?

[email protected] (John Napiorkowski)
Newsgroups perl.moose
Message-ID <[email protected]>


--- On Wed, 1/14/09, Howe, Tom (IT) <[email protected]> wrote:

> From: Howe, Tom (IT) <[email protected]>
> Subject: Possible to use coerce and auto_deref?
> To: "Sartak" <[email protected]>
> Cc: [email protected], "Yuval Kogman" <[email protected]>, "Stevan Little" <[email protected]>, [email protected]
> Date: Wednesday, January 14, 2009, 1:06 PM
> I have something like this:
> 
> subtype 'Thing'
>   => as 'Object'
>   => where { $_->isa('ThingClass') };
> 
> coerce 'Thing'
>   => from 'Str'
>   => via { ThingClass->new ( xyz=> $_ ) };
> 
> 
> has 'things' => (
>   is=>'rw',
>   isa=>'ArrayRef[Thing]',
>   lazy=>1,
>   coerce=>1,
>   auto_deref=>1,
>   default => sub { [] },
> };
> 
> 
> But if I pass in a list of strings to convert to Things, I
> get the error:
> 
> "Cannot coerce without a type coercion"
> 
> I've managed to get around it by defining a type
> 'ArrayRef[Thing]'
> 
> Eg
> Subtype 'ArrayRef[Thing]'
>   => as 'ArrayRef'
>   => where {
>         foreach (@{$_} ....
>      };
> 
> Coerce 'ArrayRef[Thing]'
>   => from 'ArrayRef'
> 
> But this means
> A) I'm essentially reimplementing ArrayRef each time.
> B) It always coerces!
> 
> It should ideally check before it coerces...
> 
> Anything I can do to improve on this?
> 
> 
> 
> 
> 
> Also, I'd like to be able to declare something like
> 
> has 'foo' => ( is=>'rw',
> delegate=> sub { Foo->instance }, handles =>[qw/x y
> z/] )
> 
> Where, if no value is passed in to foo() on construction,
> the accessor created for foo() will always trigger the
> delegate sub but will not store anything in the object in
> the way default does.
> 
> 
> I tried this ..
> 
> has 'foo' => ( is=>'rw',
> isa=>'Object', handles=> [qw/meth1 meth2
> meth3/]);
> 
> around 'foo' => sub {
>   my ($next,$self,@args) = @_;
>   if (@args) {
>     return $self->$next(@args);
>   } else {
>     return $self->$next() || Foo->instance();
>   }
> };
> 
> But got error:
> 
> Cannot delegate meth1 to meth1 because the value of foo is
> not defined...
> 
> 
> Is there some way to do this?
> 
> Thanks
> 
> Tom
> 
> 
> 
> > -----Original Message-----
> > From: Sartak [mailto:[email protected]]
> > Sent: 07 January 2009 12:39
> > To: Howe, Tom (IT)
> > Cc: [email protected]; Yuval Kogman; Stevan Little;
> [email protected]
> > Subject: Re: Possible to disable/rename meta() method
> >
> > On Wed, Jan 7, 2009 at 4:49 AM, Howe, Tom (IT)
> > <[email protected]> wrote:
> > > Would be nice to be able to optionally rename or
> prefix it.
> > >
> > > I don't think a object meta class should add
> methods to the
> > main interface unless the object specifically needs to
> expose them.
> > >
> > > So by default there could be a _moose_meta()
> method and an
> > easy way to create a meta() -> _moose_meta()
> handler if
> > required (per class). This could be a global option if
> want
> > to retain compatibility with existing modules that
> require it.
> >
> > Yes, we all agree that it would be useful to give
> users the
> > ability to rename or not install a meta. The problem
> is that
> > in quite a few places in Class::MOP, Moose, and all of
> > MooseX, we call $pkg->meta with the expectation
> that it'll be
> > there and return the metaclass.
> >
> > It's just a small design problem, one we can fix
> with enough
> > grunt work. :)
> >
> > >
> > > Thanks
> >
> > Shawn
> >
> > > Ps. How do I subscribe to the mail list?
> >
> > Send mail to [email protected].
> >
> --------------------------------------------------------
> 
> NOTICE: If received in error, please destroy and notify
> sender. Sender does not intend to waive confidentiality or
> privilege. Use of this email is prohibited when received in
> error.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.