{
    "content": [
        {
            "type": "text",
            "text": "# Type::Tiny::Manual::UsingWithMouse (perldoc)\n\n## NAME\n\nType::Tiny::Manual::UsingWithMouse - how to use Type::Tiny with Mouse\n\n## Sections\n\n- **NAME**\n- **MANUAL** (3 subsections)\n- **NEXT STEPS**\n- **AUTHOR**\n- **COPYRIGHT AND LICENCE**\n- **DISCLAIMER OF WARRANTIES**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Type::Tiny::Manual::UsingWithMouse",
        "section": "",
        "mode": "perldoc",
        "summary": "Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny with Mouse",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "MANUAL",
                "lines": 41,
                "subsections": [
                    {
                        "name": "Type::Utils",
                        "lines": 54
                    },
                    {
                        "name": "Type::Tiny and MouseX::Types",
                        "lines": 49
                    },
                    {
                        "name": "Type::Tiny Performance",
                        "lines": 11
                    }
                ]
            },
            {
                "name": "NEXT STEPS",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENCE",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DISCLAIMER OF WARRANTIES",
                "lines": 4,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny with Mouse\n",
                "subsections": []
            },
            "MANUAL": {
                "content": "First read Type::Tiny::Manual::Moo, Type::Tiny::Manual::Moo2, and Type::Tiny::Manual::Moo3.\nEverything in those parts of the manual should work exactly the same in Mouse.\n\nThis part of the manual will focus on Mouse-specifics.\n\nOverall, Type::Tiny is less well-tested with Mouse than it is with Moose and Moo, but there are\nstill a good number of test cases for using Type::Tiny with Mouse, and there are no known major\nissues with Type::Tiny's Mouse support.\n\nWhy Use Type::Tiny At All?\nMouse does have a built-in type constraint system which is fairly convenient to use, but there\nare several reasons you should consider using Type::Tiny instead.\n\n*   Type::Tiny provides helpful methods like \"where\" and \"pluscoercions\" that allow type\nconstraints and coercions to be easily tweaked on a per-attribute basis.\n\nSomething like this is much harder to do with plain Mouse types:\n\nhas name => (\nis      => \"ro\",\nisa     => Str->pluscoercions(\nArrayRef[Str], sub { join \" \", @$ },\n),\ncoerce  => 1,\n);\n\nMouse tends to encourage defining coercions globally, so if you wanted one Str attribute to\nbe able to coerce from ArrayRef[Str], then *all* Str attributes would coerce from\nArrayRef[Str], and they'd all do that coercion in the same way. (Even if it might make sense\nto join by a space in some places, a comma in others, and a line break in others!)\n\n*   Type::Tiny provides automatic deep coercions, so if type Xyz has a coercion, the following\nshould \"just work\":\n\nisa xyzlist => ( is => 'ro', isa => ArrayRef[Xyz], coerce => 1 );\n\n*   Type::Tiny offers a wider selection of built-in types.\n\n*   By using Type::Tiny, you can use the same type constraints and coercions for attributes and\nmethod parameters, in Mouse and non-Mouse code.\n",
                "subsections": [
                    {
                        "name": "Type::Utils",
                        "content": "If you've used Mouse::Util::TypeConstraints, you may be accustomed to using a DSL for declaring\ntype constraints:\n\nuse Mouse::Util::TypeConstraints;\n\nsubtype 'Natural',\nas 'Int',\nwhere { $ > 0 };\n\nThere's a module called Type::Utils that provides a very similar DSL for declaring types in\nType::Library-based type libraries.\n\npackage My::Types {\nuse Type::Library -base;\nuse Type::Utils;\nuse Types::Standard qw( Int );\n\ndeclare 'Natural',\nas Int,\nwhere { $ > 0 };\n}\n\nPersonally I prefer the more object-oriented way to declare types though.\n\nIn Mouse you might also declare types like this within classes and roles too. Unlike Mouse,\nType::Tiny doesn't keep types in a single global flat namespace, so this doesn't work quite the\nsame with Type::Utils. It still creates the type, but it doesn't store it in any type library;\nthe type is returned.\n\npackage My::Class {\nuse Mouse;\nuse Type::Utils;\nuse Types::Standard qw( Int );\n\nmy $Natural =          # store type in a variable\ndeclare 'Natural',\nas Int,\nwhere { $ > 0 };\n\nhas number => ( is => 'ro', isa => $Natural );\n}\n\nBut really, isn't the object-oriented way cleaner?\n\npackage My::Class {\nuse Mouse;\nuse Types::Standard qw( Int );\n\nhas number => (\nis   => 'ro',\nisa  => Int->where('$ > 0'),\n);\n}\n"
                    },
                    {
                        "name": "Type::Tiny and MouseX::Types",
                        "content": "Types::Standard should be a drop-in replacement for MooseX::Types. And Types::Common::Numeric\nand Types::Common::String should easily replace MouseX::Types::Common::Numeric and\nMouseX::Types::Common::String.\n\nThat said, if you do with to use a mixture of Type::Tiny and MouseX::Types, they should fit\ntogether pretty seamlessly.\n\nuse Types::Standard qw( ArrayRef );\nuse MouseX::Types::Mouse qw( Int );\n\n# this should just work\nmy $listofnums = ArrayRef[Int];\n\n# and this\nmy $listornum = ArrayRef | Int;\n\n\"-mouse\" Import Parameter\nIf you have read this far in the manual, you will know that this is the usual way to import type\nconstraints:\n\nuse Types::Standard qw( Int );\n\nAnd the \"Int\" which is imported is a function that takes no arguments and returns the Int type\nconstraint, which is a blessed object in the Type::Tiny class.\n\nType::Tiny mocks the Mouse::Meta::TypeConstraint API so well that most Mouse and MouseX code\nwill not be able to tell the difference.\n\nBut what if you need a real Mouse::Meta::TypeConstraint object?\n\nuse Types::Standard -mouse, qw( Int );\n\nNow the \"Int\" function imported will return a genuine native Mouse type constraint.\n\nThis flag is mostly a throwback from when Type::Tiny native objects *didn't* directly work in\nMouse. In 99.9% of cases, there is no reason to use it and plenty of reasons not to. (Mouse\nnative type constraints don't offer helpful methods like \"pluscoercions\" and \"where\".)\n\n\"mousetype\" Method\nAnother quick way to get a native Mouse type constraint object from a Type::Tiny object is to\ncall the \"mousetype\" method:\n\nuse Types::Standard qw( Int );\n\nmy $tinytype   = Int;\nmy $mousetype  = $tinytype->mousetype;\n\nInternally, this is what the \"-mouse\" flag makes imported functions do.\n"
                    },
                    {
                        "name": "Type::Tiny Performance",
                        "content": "Type::Tiny should run pretty much as fast as Mouse types do. This is because, when possible, it\nwill use Mouse's XS implementations of type checks to do the heavy lifting.\n\nThere are a few type constraints where Type::Tiny prefers to do things without Mouse's help\nthough, for consistency and correctness. For example, the Mouse XS implementation of Bool is...\nstrange... it accepts blessed objects that overload \"bool\", but only if they return false. If\nthey return true, it's a type constraint error.\n\nUsing Type::Tiny instead of Mouse's type constraints shouldn't make a significant difference to\nthe performance of your code.\n"
                    }
                ]
            },
            "NEXT STEPS": {
                "content": "Here's your next step:\n\n*   Type::Tiny::Manual::UsingWithClassTiny\n\nIncluding how to Type::Tiny in your object's \"BUILD\" method, and third-party shims between\nType::Tiny and Class::Tiny.\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Toby Inkster <tobyink@cpan.org>.\n",
                "subsections": []
            },
            "COPYRIGHT AND LICENCE": {
                "content": "This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
                "subsections": []
            },
            "DISCLAIMER OF WARRANTIES": {
                "content": "THIS PACKAGE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\nWITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.\n",
                "subsections": []
            }
        }
    }
}