Skip to contents

In the methods of impl(), a function with the type of the value it returns, which fits the return type of the trait's method: impl(Train, GLMNET, methods = list(train = impl_method(fn, return_type = GLMNETModel))), where train is declared with return_type = subtype(Model). The same type fits; a struct, or subtype() of it, fits subtype() of a struct it extends; a type with an impl of the trait fits dyn() of it; and T or option(T) fits option(T). A constraint narrows nothing: type_double(min = 0) fits type_double(min = 0) only. implement(generic, Type, return_type = T) <- fn declares the same for a generic.

Usage

impl_method(fn, return_type)

Arguments

fn

Function: The method.

return_type

An r8_field_type or an R8 type: The type of the value it returns.

Value

An r8_impl_method.

See also

impl(), implement<-.

Examples

op <- options(r8.namespace = "demo")
Model <- new_struct("Model", fields = list(rss = type_double()))
LinearModel <- new_struct("LinearModel", extends = Model)
Linear <- new_struct("Linear")
Train <- new_trait("Train", methods = list(
  train = trait_method(params(spec, y), return_type = subtype(Model))
))
impl(Train, Linear, methods = list(
  train = impl_method(
    function(spec, y) LinearModel(rss = sum((y - mean(y))^2)),
    return_type = LinearModel
  )
))
Train@train(Linear(), c(1, 2, 3))
#> <demo::LinearModel>
#>   rss: 2
options(op)