Type::Const released

I've created an experimental Type::Tiny library called Type::Const that coerces ArrayRefs and HashRefs into constants using Const::Fast.


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:

has things => (
   is => 'ro',
   isa => 'BagOfWord',
   const => 1,

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.

Leave a comment

About Robert Rothenberg

user-pic I was born on the Moon but kidnapped by astronauts and raised in the suburbs of Grumman. Eventually, I drifted along the Gulf Stream to Northern Europe. Blogs about Perl, Puppet, Linux, programming, and whatever etc.