| new_interface | R Documentation |
new_interface() models the method-list part of Go interfaces as a list of
required S7 generics. An interface is just a named set of required generics,
and a class or object satisfies it when S7 can find a method for every
required generic.
new_interface(
name,
generics = list(),
parents = list(),
package = NULL,
methods = NULL
)
interface_requirement(
generic,
name = NULL,
args = list(),
returns = S7::class_any
)
name |
For |
generics |
For |
parents |
Optional interface or list of interfaces to embed. |
package |
Optional package name used only for display. |
methods |
Compatibility alias for |
generic |
An S7 generic function. |
args |
Optional named list of S7 classes, interfaces, or traits for
runtime argument checking with |
returns |
Optional S7 class, interface, or trait for runtime return
checking with |
This mirrors Go's basic interfaces defined only by methods. Define small interfaces at the point where consuming code needs a behavior. Define S7 classes, generics, and methods normally; then let consumers name the protocol they accept. Up-front interfaces are also useful for package protocols, abstract data types, or recursive protocols.
Go's full post-1.18 type-set language is outside this model, including tilde type terms, unions of concrete types, or pointer/value receiver rules.
new_interface() returns an S7 object of class
s7_interface. interface_requirement() returns an S7 object of class
s7_interface_requirement.
local({
area <- S7::new_generic("area", "x")
draw <- S7::new_generic("draw", "x")
Circle <- S7::new_class(
"Circle",
properties = list(r = S7::class_double)
)
Rect <- S7::new_class(
"Rect",
properties = list(w = S7::class_double, h = S7::class_double)
)
S7::method(area, Circle) <- function(x) pi * x@r^2
S7::method(draw, Circle) <- function(x) sprintf("circle(r = %s)", x@r)
S7::method(area, Rect) <- function(x) x@w * x@h
Drawable <- new_interface("Drawable", generics = list(draw = draw))
Shape <- new_interface("Shape", generics = list(area = area), parents = Drawable)
implements(Circle, Shape)
missing_requirements(Rect, Shape)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.