-
Notifications
You must be signed in to change notification settings - Fork 34
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
convert() should be idempotent when from
is an instance of to
#429
Comments
Just realized the problem is worse: if The methods package seems to get this right, but the implementation of the logic probably rivals the entire S7 package in terms of lines of code. Perhaps something like this would work inside from_dispatch <- obj_dispatch(from)
to_class <- class_register(to)
if (inherits(from, to))
from_dispatch <- head(from_dispatch, match(to_class, from_dispatch) - 1L)
if (length(from_dispatch) == 0L)
return(from)
dispatch <- list(from_dispatch, to_class)
convert <- .Call(method_, convert, dispatch, environment(), FALSE) Or a bit more efficiently: from_dispatch <- obj_dispatch(from)
to_class <- class_register(to)
m <- match(to_class, from_dispatch)
if (!is.na(m)) # upcasting case: avoid downcasting methods
from_dispatch <- head(from_dispatch, m - 1L)
if (length(from_dispatch) == 0L)
return(from)
dispatch <- list(from_dispatch, to_class)
convert <- .Call(method_, convert, dispatch, environment(), FALSE) |
Just a note that the above solution does not preclude dispatch to Another note: this change would also prevent the current loophole of:
which a devious person, like me, might define to make |
Was this resolved by #444? |
No, I don't think so. This relates to selecting a Btw, I think the |
Link to |
If
from
is an instance ofto
, i.e.,identical(S7_class(from), to)
isTRUE
(assumingfrom
is an S7 object), then no dispatch should occur.This is not (just) about efficiency. If there are
convert()
methods for downcasting toto
, one of them will be called even if there is no need to downcast, potentially resulting in data being overwritten, or other weird things happening.I'll also note that
methods::as()
is idempotent whento
isclass_any
, butclass_any
is not allowed withconvert()
. Somewhat pedantic, but I thought it was worth mentioning.The text was updated successfully, but these errors were encountered: