NAME

Lexical::TypeTiny - my Int $n

SYNOPSIS

  use Types::Standard qw(Int);
  use Lexical::TypeTiny;
  
  my Int $n = 42;
  $n += 0.5;   # dies, not an Int

DESCRIPTION

Lexical::TypeTiny is similar in spirit to Type::Tie, but:

  • It's a lot faster because it uses Variable::Magic instead of tie.

  • It's limited to only scalar variables, no arrays or hashes. (Of course, those scalars may be arrayrefs or hashrefs.)

  • Does not (currently) support coercion.

  • It's limited to simple type constraints like ArrayRef, and not parameterized type constraints like ArrayRef[Int]. (This is a limitation of the syntax Perl will parse, not a limitation of the complexity of type constraints supported. You can define a ArrayRef_of_Int type constraint in your own type library, and it will work.)

  • Although an exception is thrown if you try to assign an invalid value to the variable, the assignment still happens. In the "SYNOPSIS", if you caught the exception and then examined $n, it would be 42.5.

    (This particular aspect of Lexical::TypeTiny's behaviour is not fixed in stone and may change in a future version.)

Because of the way Perl parses lexical variable types, if you wish to declare, say my Int $x, there needs to exist a class called Int. That class doesn't have to actually do anything; it doesn't need constructors, methods, etc.

Lexical::TypeTiny will create such classes for you at import time, however to do so, it needs to know what type constraints you are planning on using. This means you need to import your type libraries before importing Lexical::TypeTiny.

Good:

  use Types::Standard qw(Int);
  use Lexical::TypeTiny;

Bad:

  use Lexical::TypeTiny;
  use Types::Standard qw(Int);

Disabling Type Checks

  use Lexical::TypeTiny -nocheck;

BUGS

There currently seem to be issues with threaded Perls. Hopefully these can be solved pretty soon.

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

SEE ALSO

Type::Tie, Types::Standard.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2018 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.