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

Please add chaining mutators #135

Open
leonerd opened this issue May 1, 2020 · 2 comments
Open

Please add chaining mutators #135

leonerd opened this issue May 1, 2020 · 2 comments

Comments

@leonerd
Copy link

leonerd commented May 1, 2020

It's a common form to want to return a newly-constructed object with all the values set into it already, by having mutator methods that return the invocant, so you can easily add more. In such a style you could write:

return HTTP::Response->new( 200 )
    ->set_content_type( "text/plain" )
    ->set_content( "Hello, world" );

As it currently stands, the existing mutator methods like ->header, ->content, etc.. do not return the invocant, thus such a style is impossible and one must use a temporary:

my $resp = HTTP::Response->new( 200 );
$resp->content_type( "text/plain" );
$resp->content( "Hello, world" );
return $resp;
@karenetheridge
Copy link
Member

Note you can also set all these things at once through the constructor -- e.g. $r = HTTP::Response->new( $code, $msg, $header, $content )

@vanHoesel
Copy link
Member

I fully agree on a approach that @leonerd describes, a very common way to do things in other languages. Bu I would strongly disagree with set_* mutators, because of 'inmutability'. I'd rather would go for with_* that would create a clone, with the new setting.

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