provides a "Self" type constraint, referring to the caller class or role


    package Cow;
    use Moo;
    package Horse;
    use Moo;
    use Types::Self;
    use Types::Standard qw( Str );
    has name   => ( is => 'ro', isa => Str  );
    has mother => ( is => 'ro', isa => Self );
    has father => ( is => 'ro', isa => Self );
  my $alice = Horse->new( name => 'Alice' );
  my $bob   = Horse->new( name => 'Bob' );
  # Okay
  my $baby = Horse->new(
    name   => 'Baby',
    mother => $alice,
    father => $bob,
  # Dies
  my $baby = Horse->new(
    name   => 'Baby',
    mother => Cow->new,
    father => $bob,



This module exports a Self type constraint which consrtains values to be blessed objects in the same class as the package it was imported into, or blessed objects which consume the role it was imported into. It should do the right thing with inheritance.

Using Self in a class means the same as InstanceOf[__PACKAGE__]. (See InstanceOf in Types::Standard.)

Using Self in a role means the same as ConsumerOf[__PACKAGE__]. (See ConsumerOf in Types::Standard.)


This module also exports is_Self, which returns a boolean.

  package Marriage;
  use Moo::Role;
  use Types::Self qw( is_Self );
  has spouse => ( is => 'rwp', init_arg => undef );
  sub marry {
    my ( $me, $maybe_spouse ) = @_;
    if ( is_Self( $maybe_spouse ) ) {
      $me->_set_spouse( $maybe_spouse );
      $maybe_spouse->_set_spouse( $me );
    else {
      warn "Cannot marry this!";
    return $me;

is_Self( $var ) can also be written as Self->check( $var ).


The module also exports assert_Self which acts like is_Self but instead of returning a boolean, either lives or dies. This can be useful is you need to check that the first argument to a function is a blessed object.

  sub connect {
    my ( $self ) = ( shift );
    assert_Self $self;  # dies if called as a class method
    $self->{connected} = 1;
    return $self;

assert_Self( $var ) can also be written as Self->( $var ).


The module also exports to_Self which will attempt to coerce other types to the Self type.

to_Self( $var ) can also be written as Self->coerce( $var ).


An easy way of adding coercions to your Self type for the benefit of to_Self. Other classes which use InstanceOf[$YourClass] will also get these coercions.

Accepts a list of type+code pairs. The code can be a scalarref naming a method to call to coerce a value, a coderef to call to coerce the value (operating on $_), or a string of Perl code to call to coerce the value (operating on $_).

  package MyClass;
  use Moo;
  use Types::Self -all;
  use Types::Standard qw( HashRef ArrayRef ScalarRef );

    HashRef,   \'new',
    ArrayRef,  \'from_array',
    ScalarRef, sub { ... },

  sub from_array {
    my ( $class, $arrayref ) = ( shift, @_ );


Only Self is exported by default.

Other functions need to be requested:

  use Types::Self -all;

Functions can be renamed:

  use Types::Self
    'Self'    => { -as => 'ThisClass' },
    'is_Self' => { -as => 'is_ThisClass' };


Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Types-Self.

See Also

Types::Standard, Type::Tiny::Manual.