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

Default constructor could take instance as first argument #229

Closed
lawremi opened this issue May 28, 2022 · 4 comments · Fixed by #296
Closed

Default constructor could take instance as first argument #229

lawremi opened this issue May 28, 2022 · 4 comments · Fixed by #296
Labels
feature a feature request or enhancement

Comments

@lawremi
Copy link
Collaborator

lawremi commented May 28, 2022

methods::initialize() takes an instance of the class (or subclass, in which case extra slots are dropped) as the first argument, and named arguments specify the slot values, overriding those of the first argument. This behavior has proven convenient for creating derivative objects. I wonder if the default constructor could behave similarly. This would often force property arguments to be named in calls to the constructor, but that at least benefits clarity.

I wonder though whether this is a feature that most benefits implementations, and so should instead be supported by a different function, like set_props(), which would just be a simple wrapper around props<-().

@hadley
Copy link
Member

hadley commented Apr 10, 2023

You can do this currently with `props<-`(obj, x = 1, y = 2) but that's an unpleasant syntax.

@hadley hadley added the feature a feature request or enhancement label Apr 10, 2023
@hadley
Copy link
Member

hadley commented Apr 17, 2023

@lawremi Which of the following syntaxes would you prefer?

set_props(obj, x = 1, y = 2)
set_props(obj, list(x = 1, y = 2))

The first is more user friendly but there's no obvious way to supply a list.

In the tidyverse we'd use "dynamic dots" here, so you could do set_props(obj, !!!a_list). I'm not sure what the base equivalent is? Add a .list argument so you can do set_props(obj, .list = a_list)?

hadley added a commit that referenced this issue Apr 17, 2023
@mmaechler
Copy link
Collaborator

Hmm, I never got why set_props() was needed when we have props(obj) <- <propslist>
and indeed the RHS could be a simple list ??
Using set_*() is really "S4-ish" and "non-R-ish" in comparison with props(.) <- ..
{yes, I must have missed something}

@hadley
Copy link
Member

hadley commented Apr 18, 2023

@mmaechler sometimes you want to create a modified copy of the object, rather than modifying in place.

hadley added a commit that referenced this issue May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants