You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Are there any plans to specify return types of a method, similar to the valueClass in the setGeneric() function? I do realize that when writing a function it's easy enough to make sure a specific return type is specified. However, it seems somewhat asymmetric that I can specify input types but not output types, it would be helpful for designing by contract, and is also helpful for code optimization.
It seems like it would be nice to have some way to declare the return type, and to check it during development. But I don't think you want to check the return type every time when calling the generic since that adds quite a lot of overhead for little gain.
Maybe R7 just needs to offer a way to declare it, and then it's up to the method implementor to fulfil the contract?
I agree about overhead. I was mostly thinking about how I'd like to be able to optimize code and if I could make guarantees about returns then I could also make guarantees when returns are arguments to functions.
Making the implementor fulfill the contract seems like it may be the least bad solution but doesn't that mean it essentially gets stored as meta-information only? I guess one could add a little bit of utility so that the implementor can check to make sure it's returning the correct type, leaving it up to the implementor to manage the overhead.
As I've thought about it more, I agree less with respect to overhead. The amount of overhead to check the return type should be very small when compared to the time to figure out which function to dispatch to. In some cases it will decrease overhead. For example, if I have a method whose return type is known and that value is used as subsequent method argument, it could reduce the number of candidate methods I could dispatch to via static code analysis.
This would be a useful feature for the reasons stated. We could defer the decision to actually check the return type. We will likely invest heavily into optimizing dispatch, which might make checking the return type a more significant portion of the runtime. Regardless, it will be optional, so the decision is left to the implementer. Since S7 provides direct property access, instead of requiring accessors, it's already possible to infer the type in many cases, with the added benefit of there being less method dispatch overall.
IMHO an "official" way to declare the return type of a method would be very useful:
From the contract perspective, it is informative for the users of the method;
It can ensure type stability (quite often the overhead of the check is negligible compared to the run time of the actual processing);
It can help method dispatch, as @kaneplusplus noted;
In IDEs or in terminal's tab completion, it can help filtering out available method/object combinations, or give warnings in case of conflicts.
I also agree with letting the actual implementation of the return type check optional and controllable.
E.g.:
never_na <- function(x) x
trace(
never_na,
exit = function() stopifnot("Return value may not contain NAs" = !anyNA(returnValue())),
print = FALSE
)
never_na(sqrt(4))
# [1] 2
never_na(sqrt(-4))
# Error in (function () : Return value may not contain NAs
# In addition: Warning message:
# In sqrt(-4) : NaNs produced
tracingState(FALSE)
never_na(sqrt(-4))
# [1] NaN
# Warning message:
# In sqrt(-4) : NaNs produced
Activity
hadley commentedon Aug 8, 2022
It seems like it would be nice to have some way to declare the return type, and to check it during development. But I don't think you want to check the return type every time when calling the generic since that adds quite a lot of overhead for little gain.
Maybe R7 just needs to offer a way to declare it, and then it's up to the method implementor to fulfil the contract?
kaneplusplus commentedon Aug 16, 2022
I agree about overhead. I was mostly thinking about how I'd like to be able to optimize code and if I could make guarantees about returns then I could also make guarantees when returns are arguments to functions.
Making the implementor fulfill the contract seems like it may be the least bad solution but doesn't that mean it essentially gets stored as meta-information only? I guess one could add a little bit of utility so that the implementor can check to make sure it's returning the correct type, leaving it up to the implementor to manage the overhead.
kaneplusplus commentedon Feb 22, 2023
As I've thought about it more, I agree less with respect to overhead. The amount of overhead to check the return type should be very small when compared to the time to figure out which function to dispatch to. In some cases it will decrease overhead. For example, if I have a method whose return type is known and that value is used as subsequent method argument, it could reduce the number of candidate methods I could dispatch to via static code analysis.
lawremi commentedon Feb 22, 2023
This would be a useful feature for the reasons stated. We could defer the decision to actually check the return type. We will likely invest heavily into optimizing dispatch, which might make checking the return type a more significant portion of the runtime. Regardless, it will be optional, so the decision is left to the implementer. Since S7 provides direct property access, instead of requiring accessors, it's already possible to infer the type in many cases, with the added benefit of there being less method dispatch overall.
tdeenes commentedon Feb 22, 2023
IMHO an "official" way to declare the return type of a method would be very useful:
I also agree with letting the actual implementation of the return type check optional and controllable.
E.g.: