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

What should coercion look like for R7? #136

Closed
hadley opened this issue Jan 17, 2022 · 5 comments · Fixed by #181
Closed

What should coercion look like for R7? #136

hadley opened this issue Jan 17, 2022 · 5 comments · Fixed by #181

Comments

@hadley
Copy link
Member

hadley commented Jan 17, 2022

No description provided.

@lawremi
Copy link
Collaborator

lawremi commented Jan 20, 2022

How about it behaves like methods::as() but with a verb like cast()? Ideally cast() would itself be a generic, with special behavior, so we can avoid the as()/coerce() duality.

Note that this would be distinct from up casting for affecting dispatch, such as when calling the next method, because casting to affect dispatch should only modify the class, not the structure (so that any extra fields are carried along), and the class modification must be transient so that any dispatch in the next method behaves correctly.

@hadley
Copy link
Member Author

hadley commented Jan 20, 2022

I think the challenge of avoiding two functions is that you want to write cast(object, class) but dispatch on class, object_class(object)).

@hadley
Copy link
Member Author

hadley commented Jan 20, 2022

(Note to self: remember to also incorporate r7_data() into this)

@lawremi
Copy link
Collaborator

lawremi commented Jan 21, 2022

Agreed that the dispatch behavior is weird, but we can implement cast() as a non-standard generic to achieve that.

@hadley
Copy link
Member Author

hadley commented Feb 15, 2022

Very initial thoughts:

cast <- R7_generic("cast", c("from", "to"), function(from, to, ...) {
  signature <- list(to = object_class(to), from = from)
  cast <- method(cast, signature)
  cast(object, ...)
})

hadley added a commit that referenced this issue Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants