{
    "content": [
        {
            "type": "text",
            "text": "# Type::Tiny::Manual::UsingWithClassTiny (info)\n\n## NAME\n\nType::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with Class::Tiny\n\n## Sections\n\n- **Type::Tiny::Manual::UsUseriContribuType::Tiny::Manual::UsingWithClassTiny(3pm)**\n- **NAME**\n- **MANUAL**\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::UsingWithClassTiny",
        "section": "",
        "mode": "info",
        "summary": "Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with Class::Tiny",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "Type::Tiny::Manual::UsUseriContribuType::Tiny::Manual::UsingWithClassTiny(3pm)",
                "lines": 1,
                "subsections": []
            },
            {
                "name": "NAME",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "MANUAL",
                "lines": 115,
                "subsections": []
            },
            {
                "name": "NEXT STEPS",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENCE",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DISCLAIMER OF WARRANTIES",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "Type::Tiny::Manual::UsUseriContribuType::Tiny::Manual::UsingWithClassTiny(3pm)": {
                "content": "",
                "subsections": []
            },
            "NAME": {
                "content": "Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with\nClass::Tiny\n",
                "subsections": []
            },
            "MANUAL": {
                "content": "Class::Tiny is an even-smaller-than-Moo class builder.\n\nLet's translate the classic Horse class from Moo to Class::Tiny.\n\nMoo:\n\npackage Horse {\nuse Moo;\nuse Types::Standard qw( Str Num ArrayRef );\nuse namespace::autoclean;\n\nhas name       => ( is => 'ro', isa => Str, required => 1 );\nhas gender     => ( is => 'ro', isa => Str );\nhas age        => ( is => 'rw', isa => Num );\nhas children   => (\nis       => 'ro',\nisa      => ArrayRef,\ndefault  => sub { return [] },\n);\n}\n\nClass::Tiny:\n\npackage Horse {\nuse Class::Tiny qw( gender age ), {\nname     => sub { die \"name is required\"; },\nchildren => sub { return [] },\n};\nuse Types::Standard qw( Str Num ArrayRef Dict Optional slurpy Any);\nuse Type::Params qw( wrapmethods compile );\nuse namespace::autoclean;\n\n# type checks\nwrapmethods(\nBUILD    => [Dict[\nname       => Str,\ngender     => Optional[Str],\nage        => Optional[Num],\nchildren   => Optional[ArrayRef],\nslurpy Any,\n]],\nname     => [],\ngender   => [],\nage      => Optional[Num],\nchildren => [],\n);\n}\n\nWhat's going on here?\n\nWell, Class::Tiny, after it has built a new object, will do this:\n\n$self->BUILD($args);\n\n(Technically, it calls \"BUILD\" not just for the current class, but for\nall parent classes too.) We can hook onto this in order to check type\nconstraints for the constructor.\n\nWe use \"wrapmethods\" from Type::Params to wrap the original \"BUILD\"\nmethod (which doesn't exist, so \"wrapmethods\" will just assume an\nempty sub) with a type check for $args. The type check is just a Dict\nthat checks the class's required and optional attributes and includes\nslurpy Any at the end to be flexible for subclasses adding new\nattributes.\n\nThen we wrap the \"name\", \"gender\", and \"children\" methods with checks\nto make sure they're only being called as getters, and we wrap \"age\",\nallowing it to be called as a setter with a Num.\n\nThere are also a couple of CPAN modules that can help you out.\n\nClass::Tiny::ConstrainedAccessor\nClass::Tiny::ConstrainedAccessor creates a \"BUILD\" and accessors that\nenforce Type::Tiny constraints.  Attribute types are passed to\nClass::Tiny::ConstrainedAccessor; attribute defaults are passed to\nClass::Tiny.\n\npackage Horse {\nuse Types::Standard qw( Str Num ArrayRef );\nuse Class::Tiny::ConstrainedAccessor {\nname     => Str,\ngender   => Str,\nage      => Num,\nchildren => ArrayRef,\n};\nuse Class::Tiny qw( gender age ), {\nname     => sub { die \"name is required\"; },\nchildren => sub { return [] },\n};\n}\n\nClass::Tiny::Antlers\nClass::Tiny::Antlers provides Moose-like syntax for Class::Tiny,\nincluding support for \"isa\".  You do not also need to use Class::Tiny\nitself.\n\npackage Horse {\nuse Class::Tiny::Antlers qw(has);\nuse Types::Standard qw( Str Num ArrayRef );\nuse namespace::autoclean;\n\nhas name       => (\nis        => 'ro',\nisa       => Str,\ndefault   => sub { die \"name is required\" },\n);\nhas gender     => ( is => 'ro',    isa => Str );\nhas age        => ( is => 'rw',    isa => Num );\nhas children   => (\nis        => 'ro',\nisa       => ArrayRef,\ndefault   => sub { return [] },\n);\n}\n",
                "subsections": []
            },
            "NEXT STEPS": {
                "content": "Here's your next step:\n\no   Type::Tiny::Manual::UsingWithOther\n\nUsing Type::Tiny with Class::InsideOut, Params::Check, and\nObject::Accessor.\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\nthe same terms as the Perl 5 programming language system itself.\n",
                "subsections": []
            },
            "DISCLAIMER OF WARRANTIES": {
                "content": "THIS PACKAGE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF\nMERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.\n\nperl v5.32.1                      2Type::Tiny::Manual::UsingWithClassTiny(3pm)",
                "subsections": []
            }
        }
    }
}