Skip to content

Feature Request: Allow to define S7 functions #564

@sebffischer

Description

@sebffischer

I want to start this issue with saying how much I enjoy programming in S7 and the ability to directly see the type signature of an object by just looking at the code and not having to read the roxygen docs or scan the function code for stopifnot(inherits(...)).

I was wondering, whether S7 could be extended with a S7_function object, which would bring the same convenience to defining functions, i.e. properly annotated type constraints.

If one were to use :=, similar to how it is discussed here, this could make function definitions look rather clean. Note that below, ClassA would not be the default, but the type of the input x.

f := function(x = ClassA, y = ClassA) {
  x + y
}

which could be internally transformed into something like below, with additional metadata stored in the attributes as is the case for S7.

function(x, y) {
  if (!inherits(x, ClassA)) stop("x must be of classA"),
  if (!inherits(y, ClassA)) stop("x must be of classA")
  x + y
}

for default arguments, one could employ the following syntax (as suggested here).

f := function(x = 1L : ClassA, y = 1L : ClassA) {
  x + y
}
f()
#> 2L

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions