-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Im looking for a way to encapsulate defaults with Type::Tiny types. #95
Comments
I did work on a branch a while back with something along those lines. You'd do: __PACKAGE__->add_type(
Type::Tiny->new(
name => 'Theme',
parent => Str,
default_generator => sub { get_default_theme() },
)
); Then: has theme => ( is => 'rw', isa => Theme, default => Theme->default_generator ); Or: has theme => Theme->spec( -rw, -default ); This providing a default would be impossible without altering how Moose/Mouse/Moo has theme => ( is => 'rw', isa => Theme ); It's certainly possible, but it's maybe a little too magic. Someone looking at the code might be confused where the default is coming from! The branch never went anywhere because I wasn't sure if it would be a feature people actually wanted or not. Even with the current release, the following should work, even though it's a bit of a hack! __PACKAGE__->add_type(
Type::Tiny->new(
name => 'Theme',
parent => Str,
default_generator => sub { get_default_theme() },
)
);
...
has theme => ( is => 'rw', isa => Theme, default => Theme->{default_generator} ); For less of a hack, subclass Type::Tiny and add a package Type::Tiny::WithDefault {
use parent 'Type::Tiny';
sub default_generator { $_[0]{default_generator} }
}
...
__PACKAGE__->add_type(
Type::Tiny::WithDefault->new(
name => 'Theme',
parent => Str,
default_generator => sub { get_default_theme() },
)
);
...
has theme => ( is => 'rw', isa => Theme, default => Theme->default_generator ); |
The next major release of Type::Tiny will include: __PACKAGE__->add_type(
Type::Tiny->new(
name => 'Theme',
parent => Str,
type_default => sub { get_default_theme() },
)
);
...
has theme => (
is => 'rw',
isa => Theme,
default => Theme->type_default,
); And some of the builtin type constraints like |
This is awesome. Thanks for adding it. Just want I was hoping for. |
I would like to do something like the following:
I know Moose, Mouse and Moo provide a default mechnism like:
But this means that every place I defined a theme in my object model I need to also add the common default. If there was a way to bundle the default with the Type::Tiny, that would be amazing.
I wondered if I just missed such a mechanism in the current Type::Tiny or if this was a new idea that had merit to add to the library?
Please advise and thanks for an awesome tool
The text was updated successfully, but these errors were encountered: