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.
- Vapour
- R
type car: object {
name: char,
speed: int
}
func (c: car) describe(): char {
return sprintf("%s goes %d km/h", c$name, c$speed)
}
describe.car = function(c) {
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.
- Vapour
- R
@generic
func (x: any) describe(...: any): char
describe = function(x, ...) {
UseMethod("describe")
}
A default method
A default method runs when no other method matches.
Declare it with @default, also on any.
- Vapour
- R
@default
func (x: any) describe(...: any): null {
stop("don't know how to describe this")
}
describe.default = function(x, ...) {
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.
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.
- Vapour
- R
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
print.car = function(c, ...) {
cat("<car>", c$name, "\n")
}
print(structure(list(name = "Herbie", speed = 120), class = c("car", "list")))
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.typein R.@genericon a bodiless method onanydeclares the generic.@defaulton a method onanydeclares 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.