- SEE ALSO
- COPYRIGHT AND LICENCE
- DISCLAIMER OF WARRANTIES
Lexical::TypeTiny - my Int $n
use Types::Standard qw(Int); use Lexical::TypeTiny; my Int $n = 42; $n += 0.5; # dies, not an Int
Lexical::TypeTiny is similar in spirit to Type::Tie, but:
It's a lot faster because it uses Variable::Magic instead of
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_Inttype 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.
use Types::Standard qw(Int); use Lexical::TypeTiny;
use Lexical::TypeTiny; use Types::Standard qw(Int);
use Lexical::TypeTiny -nocheck;
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.
Toby Inkster <firstname.lastname@example.org>.
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.
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.