stevan / p5-mop-redux

A(nother) MOP for Perl 5
139 stars 36 forks source link

Some errors in attribute declaration silently ignored #146

Closed Ovid closed 10 years ago

Ovid commented 10 years ago

I'm unsure exactly what error we have here, but consider the following:

use 5.018;
use strict;
use warnings;
use mop;

class Foo {
    has $!test = ro;    # XXX why isn't this a syntax error?
    method get_test { $!test };
}

my $foo = Foo->new( test => 'this' );
say $foo->get_test;
say $foo->test;

That prints "this", followed by "Can't locate object method 'test'...". That's because we're assigning a bareword to $!test, but in reality it's the ro trait. If we change ro to foobar, we get the expected bareword error.

If we call the constructor without args, the above returns:

Can't call method "isa" on an undefined value at /Users/ovid/perl5/perlbrew/perls/perl-5.18.1/lib/site_perl/5.18.1/darwin-2level/mop/traits.pm line 59.

This is version .03 of the mop.

Cheers, Ovid

stevan commented 10 years ago

That is not a syntax error because it is valid syntax. The default value is not evaluated until object construction, which is why you are seeing the delay. And when you try and construct the object, it is calling the ro subroutine (cause that is what traits are after all) with no arguments, which results in the error you are seeing (because traits expect a meta object as their first argument).

Only real way to solve this is to improve the error checking in the ro trait so that it handles undefs.

doy commented 10 years ago

This provides a slightly better error message now. Not sure if there's anything more we want to do here.