Looking at the code I can't see any reason why it needs its own __ANON__ prefix for the classes
so I took that out and changed the code to create_anon_class. This seems to be working fine
and does properly cache.
Attached is a patch that I believe works out.
diff --git a/lib/MooseX/Traits/Util.pm b/lib/MooseX/Traits/Util.pm
index 9dfdf96..4420d49 100644
--- a/lib/MooseX/Traits/Util.pm
+++ b/lib/MooseX/Traits/Util.pm
@@ -65,8 +65,8 @@ sub new_class_with_traits {
my $meta;
@traits = resolve_traits($class, @traits);
if (@traits) {
- $meta = $class->meta->create(
- join(q{::} => 'MooseX::Traits::__ANON__::SERIAL', ++$anon_serial),
+ $meta = $class->meta->create_anon_class(
+ #join(q{::} => 'MooseX::Traits::__ANON__::SERIAL', ++$anon_serial),
superclasses => [ $class->meta->name ],
roles => \@traits,
cache => 1,
diff --git a/t/class.t b/t/class.t
index db36937..ffda648 100644
--- a/t/class.t
+++ b/t/class.t
@@ -1,6 +1,6 @@
use strict;
use warnings;
-use Test::More tests => 6;
+use Test::More tests => 8;
use Test::Exception;
use MooseX::Traits; # for "no warnings ..."
@@ -49,6 +49,12 @@ lives_ok {
ok $class;
+my $class2;
+lives_ok {
+ $class2 = new_class_with_traits( 'Class' => 'Trait', 'Another::Trait' );
+} 'same class definition';
+is $class->name, $class2->name, 'same meta class';
+
my $instance = $class->name->new( foo => '42', bar => '24' );
is $instance->foo, 42;
is $instance->bar, 24;