Skip to main content
Version: Next

Methods

R's S3 system lets one function behave differently depending on the class of its first argument: print() prints a data frame differently from a model. Vapour keeps S3, but gives it a clearer syntax.

The problem with dots​

In R, a method is a function named generic.class:

format.data.frame <- function(x, ...) {}

Is this format for data.frame, format.data for frame, or just a function with dots in its name? You can't tell by reading it. Vapour borrows the method syntax from Go to make it explicit.

Declaring a method​

Put the value the method applies to, the receiver, in parentheses between func and the method's name.

type car: object {
name: char,
speed: int
}

func (c: car) describe(): char {
return sprintf("%s goes %d km/h", c$name, c$speed)
}

Read it as: the describe method, for values of type car. It transpiles to an S3 method, describe.car, and the receiver becomes the first argument.

Inside the method, the receiver is a typed value like any other: c$name is checked against the car type.

Declaring a generic​

For R to dispatch describe() to describe.car, a generic function describe must exist. Declare it with @generic on a method whose receiver is any, with no body.

@generic
func (x: any) describe(...: any): char

A default method​

A default method runs when no other method matches. Declare it with @default, also on any.

@default
func (x: any) describe(...: any): null {
stop("don't know how to describe this")
}

A method on any is only allowed with @generic or @default.

A complete example​

Here is a generic area() with a method for each shape and a default that fails clearly.

shapes.vp
type rect: object {
w: num,
h: num
}

type circle: object {
r: num
}

@generic
func (s: any) area(...: any): num

@default
func (s: any) area(...: any): null {
stop("area() is not implemented for this shape")
}

func (s: rect) area(...: any): num {
return s$w * s$h
}

func (s: circle) area(...: any): num {
return pi * s$r ^ 2
}

print(area(rect(w = 2, h = 3))) # 6
print(area(circle(r = 1))) # 3.141593
vapour -infile=shapes.vp -run-only

Methods for existing generics​

Declaring the generic is optional. Without @generic, Vapour only creates the method, which is what you want for generics that already exist, like print, format, or summary.

type car: object {
name: char,
speed: int
}

func (c: car) print(...: any): null {
cat("<car>", c$name, "\n")
}

print(car(name = "Herbie", speed = 120))
# <car> Herbie

Rules​

  • A method can only be defined once per type.
  • Methods dispatch on the class, so if you change the class with @class, methods dispatch on the new classes.
  • Avoid naming types after existing S3 classes, R would dispatch their methods on your values.

Recap​

  • func (x: type) name(): type { ... } declares a method, name.type in R.
  • @generic on a bodiless method on any declares the generic.
  • @default on a method on any declares the fallback.
  • To add a method to print, format, etc., just declare the method.

The S3 example shows a fuller use of methods. The precise rules are in the spec: methods.