constructor returns an object not isa('CHI')

27 views
Skip to first unread message

ether

unread,
Feb 17, 2012, 11:55:47 AM2/17/12
to Perl-Cache Discuss
perl -MCHI -MScalar::Util=blessed -wle'print blessed(CHI-
>new(driver=>"File"))'
Moose::Meta::Class::__ANON__::SERIAL::1

perl -MCHI -MScalar::Util=blessed -wle'print CHI-
>new(driver=>"File")->isa("CHI")'
<false>

Since CHI.pm is empty except for its constructor, would it be a
problem to
create the anonymous class using both the driver and CHI itself as
superclasses,
so that attribute declarations such as this would pass type
constraints?

has cache => (
is => 'ro',
isa => 'CHI',
lazy => 1,
default => sub { CHI->new(driver => shift->driver) },
);

Jonathan Swartz

unread,
Feb 19, 2012, 10:04:32 AM2/19/12
to perl-cach...@googlegroups.com
Is there a problem with doing this instead:

isa => 'CHI::Driver'


> Since CHI.pm is empty except for its constructor, would it be a
> problem to
> create the anonymous class using both the driver and CHI itself as
> superclasses,
> so that attribute declarations such as this would pass type
> constraints?
>
> has cache => (
> is => 'ro',
> isa => 'CHI',
> lazy => 1,
> default => sub { CHI->new(driver => shift->driver) },
> );
>

> --
> You received this message because you are subscribed to the Google Groups "Perl-Cache Discuss" group.
> To post to this group, send email to perl-cach...@googlegroups.com.
> To unsubscribe from this group, send email to perl-cache-disc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/perl-cache-discuss?hl=en.
>

Karen Etheridge

unread,
Feb 19, 2012, 12:46:26 PM2/19/12
to perl-cach...@googlegroups.com
On Sun, Feb 19, 2012 at 07:04:32AM -0800, Jonathan Swartz wrote:
> Is there a problem with doing this instead:
>
> isa => 'CHI::Driver'

It depends on how you define "problem". :) It's generally considered bad
practice for a constructor to return an object not of the same type as the
class it was constructed from -- it violates the principle of least
surprise.


Reply all
Reply to author
Forward
0 new messages