{"id":16307559,"url":"https://github.com/kfly8/type-alias","last_synced_at":"2025-08-01T07:42:06.485Z","repository":{"id":184642745,"uuid":"672244323","full_name":"kfly8/Type-Alias","owner":"kfly8","description":"type alias for type constraints","archived":false,"fork":false,"pushed_at":"2023-08-14T09:18:55.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T19:57:28.665Z","etag":null,"topics":["perl"],"latest_commit_sha":null,"homepage":"","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kfly8.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-29T12:10:19.000Z","updated_at":"2023-07-30T04:43:11.000Z","dependencies_parsed_at":"2024-11-06T03:21:53.273Z","dependency_job_id":null,"html_url":"https://github.com/kfly8/Type-Alias","commit_stats":{"total_commits":64,"total_committers":2,"mean_commits":32.0,"dds":0.265625,"last_synced_commit":"a20258d8f99407f3656348844ee1efa65b723dbe"},"previous_names":["kfly8/p5-type-alias","kfly8/type-alias"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2FType-Alias","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2FType-Alias/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2FType-Alias/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2FType-Alias/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kfly8","download_url":"https://codeload.github.com/kfly8/Type-Alias/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625510,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["perl"],"created_at":"2024-10-10T21:14:35.382Z","updated_at":"2025-04-12T19:57:31.532Z","avatar_url":"https://github.com/kfly8.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Actions Status](https://github.com/kfly8/Type-Alias/actions/workflows/test.yml/badge.svg)](https://github.com/kfly8/Type-Alias/actions) [![Coverage Status](https://img.shields.io/coveralls/kfly8/Type-Alias/main.svg?style=flat)](https://coveralls.io/r/kfly8/Type-Alias?branch=main) [![MetaCPAN Release](https://badge.fury.io/pl/Type-Alias.svg)](https://metacpan.org/release/Type-Alias)\n# NAME\n\nType::Alias - type alias for type constraints\n\n# SYNOPSIS\n\n```perl\nuse Types::Standard -types;\nuse Type::Alias\n    -alias =\u003e [qw(ID User Guest LoginUser UserList)],\n    -fun =\u003e [qw(List)];\n\ntype ID =\u003e Str;\n\ntype LoginUser =\u003e {\n    _type =\u003e 'LoginUser',\n    id   =\u003e ID,\n    name =\u003e Str,\n    age  =\u003e Int,\n};\n\ntype Guest =\u003e {\n    _type =\u003e 'Guest',\n    name =\u003e Str,\n};\n\ntype User =\u003e LoginUser | Guest;\n\ntype List =\u003e sub {\n    my ($R) = @_;\n    $R ? ArrayRef[$R] : ArrayRef;\n};\n\ntype UserList =\u003e List[User];\n\nUserList-\u003echeck([\n    { _type =\u003e 'LoginUser', id =\u003e '1', name =\u003e 'foo', age =\u003e 20 },\n    { _type =\u003e 'Guest', name =\u003e 'bar' },\n]); # =\u003e OK\n\n# Internally UserList is equivalent to the following type:\n#\n# ArrayRef[\n#     Dict[\n#         _type =\u003e Eq['LoginUser'],\n#         age =\u003e Int,\n#         id =\u003e Str,\n#         name =\u003e Str\n#     ] |\n#     Dict[\n#         _type =\u003e Eq['Guest'],\n#         name =\u003e Str\n#     ]\n# ]\n```\n\n# DESCRIPTION\n\nType::Alias creates type aliases and type functions for existing type constraints such as Type::Tiny, Moose, Mouse. The aim of this module is to enhance the reusability of types and make it easier to express types.\n\n## IMPORT OPTIONS\n\n### -alias\n\n`-alias` is an array reference that defines type aliases. The default is `[]`.\n\n```perl\nuse Type::Alias -alias =\u003e [qw(ID User)];\n\ntype ID =\u003e Str;\n\ntype User =\u003e {\n    id   =\u003e ID,\n    name =\u003e Str,\n    age  =\u003e Int,\n};\n```\n\n### -fun\n\n`-fun` is an array reference that defines type functions. The default is `[]`.\n\n```perl\nuse Type::Alias -fun =\u003e [qw(List)];\n\ntype List =\u003e sub($R) {\n   $R ? ArrayRef[$R] : ArrayRef;\n};\n```\n\n### type\n\nThe `type` option is used to configure the type function that defines type aliases and type functions.\n\n```perl\n# Rename type function:\nuse Type::Alias type =\u003e { -as =\u003e 'mytype' };\n\nmytype ID =\u003e Str; # declare type alias\n```\n\n## EXPORTED FUNCTIONS\n\n### type($alias\\_name, $type\\_args)\n\n`type` is a function that defines a type alias and a type function.\nIt recursively generates type constraints based on `$type_args`.\n\n#### `$type_args` is a type constraint\n\nGiven a type constraint in `$type_args`, it returns the type constraint as is.\nType::Alias treats objects with `check` and `get_message` methods as type constraints.\n\n```perl\ntype ID =\u003e Str;\n\nID-\u003echeck('foo'); # OK\n```\n\nInternally `ID` is equivalent to the following type:\n\n```perl\nsub ID() { Str }\n```\n\n#### `$type_args` is an undefined value\n\nGiven a undefined value in `$type_args`, it returns the type constraint defined by Type::Tiny's Undef type.\n\n```perl\ntype Foo =\u003e Undef;\n\nFoo-\u003echeck(undef); # OK\n```\n\nInternally `Foo` is equivalent to the following type:\n\n```perl\nsub Foo() { Undef }\n```\n\n#### `$type_args` is a string value\n\nGiven a string value in `$type_args`, it returns the type constraint defined by [Types::Equal::Eq](https://metacpan.org/pod/Types%3A%3AEqual%3A%3AEq) type.\n\n```perl\ntype ID =\u003e 'foo';\n\nID-\u003echeck('foo'); # OK\n\ntype Published =\u003e 'published';\ntype Draft =\u003e 'draft';\ntype Status =\u003e Published | Draft;\n\nStatus-\u003echeck('published'); # ok\nStatus-\u003echeck('draft'); # ok\n```\n\nInternally `Status` is equivalent to the following type:\n\n```perl\nsub Status() { Eq['published'] | Eq['draft'] }\n```\n\n#### `$type_args` is a number value\n\n**Available at v5.36 above. Less than v5.36, converts to Eq.**\n\nGiven a number value in `$type_args`, it returns the type constraint defined by [Types::Equal::NumEq](https://metacpan.org/pod/Types%3A%3AEqual%3A%3ANumEq) type.\n\n```perl\ntype Foo =\u003e 123;\n# Foo is NumEq[123]; v5.36 above\n# Foo is Eq[123]; # less than v5.36\n```\n\n#### `$type_args` is a boolean value\n\n**Available at v5.36 above. Less than v5.36, converts to Eq.**\n\nGiven a boolean value in `$type_args`, it returns the type constraint defined by Type::Tiny's Bool type.\n\n```perl\ntype Foo =\u003e !!1;\n# Foo is Type::Alias::True; v5.36 above\n# Foo is Eq[!!1]; # less than v5.36\n```\n\n#### `$type_args` is a hash reference\n\nGiven a hash reference in `$type_args`, it returns the type constraint defined by Type::Tiny's Dict type.\n\n```perl\ntype Point =\u003e {\n    x =\u003e Int,\n    y =\u003e Int,\n};\n\nPoint-\u003echeck({\n    x =\u003e 1,\n    y =\u003e 2\n}); # OK\n```\n\nInternally `Point` is equivalent to the following type:\n\n```perl\nsub Point() { Dict[x=\u003eInt,y=\u003eInt] }\n```\n\n#### `$type_args` is an array reference\n\nGiven an array reference in `$type_args`, it returns the type constraint defined by Type::Tiny's Tuple type.\n\n```perl\ntype Option =\u003e [Str, Int];\n\nOption-\u003echeck('foo', 1); # OK\n```\n\nInternally `Option` is equivalent to the following type:\n\n```perl\nsub Option() { Tuple[Str,Int] }\n```\n\n#### `$type_args` is a code reference\n\nGiven a code reference in `$type_args`, it defines a type function that accepts a type constraint as an argument and returns the type constraint.\n\n```perl\ntype List =\u003e sub($R) {\n   $R ? ArrayRef[$R] : ArrayRef;\n};\n\ntype Points =\u003e List[{ x =\u003e Int, y =\u003e Int }];\n\nPoints-\u003echeck([\n    { x =\u003e 1, y =\u003e 2 },\n    { x =\u003e 3, y =\u003e 4 },\n]); # OK\n```\n\nInternally `List` is equivalent to the following type:\n\n```perl\nsub List :prototype(;$) {\n   my @args = map { Type::Alias::to_type($_) } @{$_[0]};\n\n    sub($R) {\n       $R ? ArrayRef[$R] : ArrayRef;\n    }-\u003e(@args);\n}\n```\n\nAnd `Points` is equivalent to the following type:\n\n```perl\nsub Points() { List[Dict[x=\u003eInt,y=\u003eInt]] }\n```\n\n# COOKBOOK\n\n## Exporter\n\nType::Alias is designed to be used with Exporter. The following is an example of using Type::Alias with Exporter.\n\n```perl\npackage MyService {\n\n    use Exporter 'import';\n    our @EXPORT_OK = qw(hello Message);\n\n    use Type::Alias -alias =\u003e [qw(Message)];\n    use Types::Common -types;\n\n    type Message =\u003e StrLength[1, 100];\n\n    sub hello { ... }\n}\n\npackage MyApp {\n\n    use MyService qw(Message);\n    Message-\u003echeck('World!');\n}\n```\n\n## Class builders\n\nType::Alias is designed to be used with class builders such as [Moose](https://metacpan.org/pod/Moose), [Moo](https://metacpan.org/pod/Moo) and [Mouse](https://metacpan.org/pod/Mouse).\n\n```perl\npackage Sample {\n    use Moose;\n\n    use Exporter 'import';\n    our @EXPORT_OK = qw( UserName );\n\n    use Type::Alias -alias =\u003e [qw( UserName )];\n    use Types::Standard qw( Str );\n\n    type UserName =\u003e Str \u0026 sub { length $_ \u003e 1 };\n\n    has 'name' =\u003e (is =\u003e 'rw', isa =\u003e UserName);\n}\n\npackage MyApp {\n\n    use Sample qw( UserName );\n\n    my $sample = Sample-\u003enew(name =\u003e 'hello');\n    $sample-\u003ehello; # =\u003e 'hello'\n    $sample-\u003ehello(''); # ERROR!\n\n    UserName-\u003echeck('hello'); # OK\n}\n```\n\n## Validation modules\n\nType::Alias is designed to be used with validation modules such as [Type::Params](https://metacpan.org/pod/Type%3A%3AParams), [Smart::Args::TypeTiny](https://metacpan.org/pod/Smart%3A%3AArgs%3A%3ATypeTiny) and [Data::Validator](https://metacpan.org/pod/Data%3A%3AValidator):\n\n```perl\nuse Type::Alias -alias =\u003e [qw( Message )];\nuse Types::Standard qw( Str );\nuse Type::Params -sigs;\n\ntype Message =\u003e Str \u0026 sub { length($_) \u003e 1 };\n\nsignature_for hello =\u003e (\n    positional =\u003e [ Message ],\n);\n\nsub hello {\n    my ($message) = @_;\n    return \"HELLO \" . $message;\n}\n\nhello('World') # =\u003e 'HELLO World';\nhello('') # =\u003e Error!\n```\n\n### NOTE\n\n[Function::Parameters](https://metacpan.org/pod/Function%3A%3AParameters) works using type aliases from outside.\n\n```perl\npackage Sample {\n\n    use Exporter 'import';\n    our @EXPORT_OK = qw(User);\n\n    use Type::Alias -alias =\u003e [qw(User)];\n    use Types::Standard -types;\n\n    type User =\u003e {\n        name =\u003e Str,\n    };\n}\n\nuse Types::Standard -types;\nuse Function::Parameters;\n\nuse Sample qw(User);\n\nfun hello (User $user) {\n    return \"Hello, $user-\u003e{name}!\";\n}\n\nhello({ name =\u003e 'foo' }) # =\u003e 'Hello, foo!';\n```\n\nHowever, if you write a type alias inline as follows, the current implementation will not work.\n\n```perl\nuse Type::Alias -alias =\u003e [qw(Gorilla)];\n\ntype Gorilla =\u003e Dict[ name =\u003e Str ];\n\nfun ooh(Gorilla $user) { # =\u003e ERROR: type Gorilla is not defined at compile time\n    return \"ooh ooh, $user-\u003e{name}!\";\n}\n\nooh({ name =\u003e 'gorilla' }) # =\u003e 'ooh ooh, gorilla!';\n```\n\n# SEE ALSO\n\n[Type::Tiny](https://metacpan.org/pod/Type%3A%3ATiny)\n\n# LICENSE\n\nCopyright (C) kobaken.\n\nThis library is free software; you can redistribute it and/or modify\nit under the same terms as Perl itself.\n\n# AUTHOR\n\nkobaken \u003ckfly@cpan.org\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Ftype-alias","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkfly8%2Ftype-alias","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Ftype-alias/lists"}