Subject: | InstanceOf[Class::Name] is not cached, makes declaring coercion inconsistent |
InstanceOf[Class::Name] is cached, but only for about a second. Then a new instance of the type is returned. This one liner demonstrates.
$ perl -wle 'use Scalar::Util qw(refaddr); use Types::Standard qw(:types); while(1) { print join ", ", (refaddr(InstanceOf["Class::Name"]), refaddr(InstanceOf["Class::Name"])); sleep 1 }'
140722877312264, 140722877312264
140722876835704, 140722876835704
140722876589416, 140722876589416
140722875485312, 140722875485312
Contrast with Str which always returns the same type object.
$ perl -wle 'use Scalar::Util qw(refaddr); use Types::Standard qw(:types); while(1) { print join ", ", (refaddr(Str), refaddr(Str)); sleep 1 }'
140414620306936, 140414620306936
140414620306936, 140414620306936
140414620306936, 140414620306936
140414620306936, 140414620306936
This seems wasteful. On a practical level, it makes adding a coercion to InstanceOf["Class::Name"] impossible.
coerce InstanceOf["URI"],
from Str,
via {
require URI;
return URI->new($_);
};
The coercion is applied to a particular instance of InstanceOf["URI"] passed to coerce. Later InstanceOf["URI"] will return a different instance with no coercion. This is confusing.
The problem appears to stem from a cache which will only hold onto externally referenced types. If I store InstanceOf["URI"] in a lexical then InstanceOf["URI"] will return the same object.
I'm having deja vu about this issue.
$ perl -wle 'use Scalar::Util qw(refaddr); use Types::Standard qw(:types); while(1) { print join ", ", (refaddr(InstanceOf["Class::Name"]), refaddr(InstanceOf["Class::Name"])); sleep 1 }'
140722877312264, 140722877312264
140722876835704, 140722876835704
140722876589416, 140722876589416
140722875485312, 140722875485312
Contrast with Str which always returns the same type object.
$ perl -wle 'use Scalar::Util qw(refaddr); use Types::Standard qw(:types); while(1) { print join ", ", (refaddr(Str), refaddr(Str)); sleep 1 }'
140414620306936, 140414620306936
140414620306936, 140414620306936
140414620306936, 140414620306936
140414620306936, 140414620306936
This seems wasteful. On a practical level, it makes adding a coercion to InstanceOf["Class::Name"] impossible.
coerce InstanceOf["URI"],
from Str,
via {
require URI;
return URI->new($_);
};
The coercion is applied to a particular instance of InstanceOf["URI"] passed to coerce. Later InstanceOf["URI"] will return a different instance with no coercion. This is confusing.
The problem appears to stem from a cache which will only hold onto externally referenced types. If I store InstanceOf["URI"] in a lexical then InstanceOf["URI"] will return the same object.
I'm having deja vu about this issue.