Type::Const released
I've created an experimental Type::Tiny library called Type::Const that coerces ArrayRefs and HashRefs into constants using Const::Fast.
Why?
When you have read-only attributes in Moo(se) classes:
has things => (
is => 'ro',
isa => HashRef,
);
While you can't change what hash reference things refers to, you can change the contents. This is fine:
$obj->things->{$key} = $val;
and there are good reasons for that.
But in other situations, you have built a table of data that you do not want changed. You can use this instead:
has things => (
is => 'ro',
isa => ConstHashRef,
coerce => 1,
);
The coercion means that any hash reference will be made read-only.
I like the idea ! :)
It also seems to me that there can also be at attribute trait declaring that "this attribute is const-y" on the types that are already defined.
IOW I'm pondering about making the following syntax work:
The "const => 1" part is the syntax provided by a trait, which applies the coercion for existing type.
This way users gets to apply this concept to ArrayRef/HashRef-based types that are already defined in his/her code.
I've updated it in v0.3.0 so that there is a single Const type that takes an optional parameter, e.g. Const[ArrayRef[Int]]
I have removed the ConstArrayRef and ConstHashref types as well.
I would have thought something like
has things => (
is => 'const',
isa => BagOfWind,
);
It should be fairly easy to write something automatically modifies the type and adds a coercion.