Repository navigation
Ensure Ops falls back to base behaviour - #382
Merged
Merged
Conversation
DavisVaughan
approved these changes
Nov 29, 2023
Comment on lines
+19
to
+22
| cnd <- tryCatch( | ||
| return(base_ops[[.Generic]](e1, e2)), | ||
| methodNotFound = function(cnd) cnd | ||
| ) |
Collaborator
There was a problem hiding this comment.
I tried to think of any other way to do this to avoid the non-negligible performance hit of tryCatch(), but I can't really think of one:
- We don't have an easy to do
findMethod(.Generic, e1 = e1, e2 = e2)so we can't try looking it up without calling it - I don't think we can easily use the more performant
withCallingHandlers()without more effort, since we don't always rethrow the error
Member
Author
There was a problem hiding this comment.
Given the general cost of dispatch I'm not sure this is worth worrying too much about?
library(S7)
foo <- new_class("foo")
method(`+`, list(foo, foo)) <- function(e1, e2) 100
f <- foo()
bench::mark(
f + f,
tryCatch(f + f)
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f + f 5.21µs 5.78µs 167089. 39.9KB 50.1
#> 2 tryCatch(f + f) 6.76µs 7.42µs 132355. 12.4KB 53.0Created on 2023-11-29 with reprex v2.0.2.9000
#Conflicts: # NEWS.md # R/method-dispatch.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #320
I also tried this:
But that leaves us with (e.g.) incompatible methods ("Ops.S7_object", "Ops.factor") for "+".