Skip to content
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

Should custom accessors be called for constructor args? #23

Open
dagolden opened this issue Apr 19, 2015 · 3 comments
Open

Should custom accessors be called for constructor args? #23

dagolden opened this issue Apr 19, 2015 · 3 comments

Comments

@dagolden
Copy link
Collaborator

If someone has custom accessors with coercion, currently the accessor won't
be called and the coercion thus won't be applied to constructor arguments,
which are directly blessed into the object hash.

Should they be called if we can detect custom accessors? If not, we should
document a pattern to help people do this, such as in BUILD:

sub BUILD {
    my $self = shift;

    $self->$_( $self->{$_} ) for qw/baz bam/; # coerce custom
}

I worry that there's no way to know that a custom accessor is also a
mutator, so leaving it to the end user in BUILD might be the safest thing.

CC: @miyagawa

@djerius
Copy link

djerius commented Oct 11, 2016

This would be nice (it would be Moo compatible), except that as things are currently written, it would prevent read-only custom accessors. They'd have to be generated within Class::Tiny itself.

@cxw42
Copy link

cxw42 commented Mar 20, 2019

I am also curious about this as part of cxw42/Class-Tiny-ConstrainedAccessor#4. Even letting BUILD know which parameters were indeed constructor parameters would be helpful (always just the actual hashtable keys in the blessed hashref, right?).

@tobyink
Copy link
Contributor

tobyink commented Mar 20, 2019

BUILD does get passed $args as a parameter. This only includes arguments passed to the constructor (technically it returns the hashref produced by BUILDARGS) and not defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants