Skip to content

Specifying a method return type #247

Open

Description

@kaneplusplus

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.

Activity

  1. hadley commented on Aug 8, 2022

    @hadley
    Member

    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?

  2. kaneplusplus commented on Aug 16, 2022

    @kaneplusplus
    Author

    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.

  3. kaneplusplus commented on Feb 22, 2023

    @kaneplusplus
    Author

    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.

  4. lawremi commented on Feb 22, 2023

    @lawremi
    Collaborator

    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.

  5. tdeenes commented on Feb 22, 2023

    @tdeenes

    IMHO an "official" way to declare the return type of a method would be very useful:

    1. From the contract perspective, it is informative for the users of the method;
    2. It can ensure type stability (quite often the overhead of the check is negligible compared to the run time of the actual processing);
    3. It can help method dispatch, as @kaneplusplus noted;
    4. 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
    
    
  6. added
    feature
    on Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discuss 🗣feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions