extend                 package:R.oo                 R Documentation

_E_x_t_e_n_d_s _a _o_b_j_e_c_t

_D_e_s_c_r_i_p_t_i_o_n:

     Simply speaking this method "extends" the class of an object. What
     is actually happening is that it creates an instance of class name
     '...className', by taking another object and add '...className' to
     the class list and also add all the named values in '...' as
     attributes.

     The method should be used by the constructor of a class and
     nowhere else.

_U_s_a_g_e:

     ## Default S3 method:
     extend(this, ...className, ...)

_A_r_g_u_m_e_n_t_s:

    this: Object to be extended.

...className: The name of new class.

     ...: Attribute fields of the new class.

_V_a_l_u_e:

     Returns an object of class '...className'.

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_E_x_a_m_p_l_e_s:

     setConstructorS3("MyDouble", function(value=0, ...) {
       extend(as.double(value), "MyDouble", ...)
     })

     setMethodS3("as.character", "MyDouble", function(object, ...) {
       fmtstr <- attr(object, "fmtstr")
       if (is.null(fmtstr))
         fmtstr <- "%.6f"
       sprintf(fmtstr, object)
     })

     setMethodS3("print", "MyDouble", function(object, ...) {
       print(as.character(object), ...)
     })

     x <- MyDouble(3.1415926)
     print(x)

     x <- MyDouble(3.1415926, fmtstr="%3.2f")
     print(x)
     attr(x, "fmtstr") <- "%e"
     print(x)




     setConstructorS3("MyList", function(value=0, ...) {
       extend(list(value=value, ...), "MyList")
     })

     setMethodS3("as.character", "MyList", function(object, ...) {
       fmtstr <- object$fmtstr
       if (is.null(fmtstr))
         fmtstr <- "%.6f"
       sprintf(fmtstr, object$value)
     })

     setMethodS3("print", "MyList", function(object, ...) {
       print(as.character(object), ...)
     })

     x <- MyList(3.1415926)
     print(x)
     x <- MyList(3.1415926, fmtstr="%3.2f")
     print(x)
     x$fmtstr <- "%e"
     print(x)

