setMethodS3               package:R.oo               R Documentation

_C_r_e_a_t_e_s _a _m_e_t_h_o_d _f_o_r _a _c_l_a_s_s _u_s_i_n_g _S_3/_U_s_e_M_e_t_h_o_d _s_t_y_l_e

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

     Creates a method for a class using S3/UseMethod style. A function
     with name 'name.class' will be set to 'definition'. The method
     will get the modifiers specified by 'modifiers'. If there exists
     no generic function for this method, it will be created
     automatically.

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

     ## Default S3 method:
     setMethodS3(name, class="default", definition, private=FALSE, protected=FALSE, static=FALSE, abstract=FALSE, trial=FALSE, deprecated=FALSE, envir=parent.frame(), overwrite=TRUE, conflict=c("warning", "error", "quiet"), createGeneric=TRUE, appendVarArgs=TRUE, enforceRCC=TRUE, ...)

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

    name: The name of the method.

   class: The class for which the method should be defined. If 'class
          == "default"' (or 'class == "ANY"') a function with name
          'name.default' will be created.

definition: The method defintion.

private, protected: If 'private=TRUE', the method is declared private.
          If 'protected=TRUE', the method is declared protected. In all
          other cases the method is declared public.

  static: If 'TRUE' this method is defined to be static, otherwise not.
          Currently this has no effect expect as an indicator.

abstract: If 'TRUE' this method is defined to be abstract, otherwise
          not. Currently this has no effect expect as an indicator.

   trial: If 'TRUE' this method is defined to be a trial method,
          otherwise not. A trial method is a method that is introduced
          to be tried out and it might be modified, replaced or even
          removed in a future release. Some people prefer to call trial
          versions, beta version. Currently this has no effect expect
          as an indicator.

deprecated: If 'TRUE' this method is defined to be deprecated,
          otherwise not. Currently this has no effect expect as an
          indicator.

   envir: The environment for where this method should be stored.

overwrite: If 'TRUE' an already existing method with the same name (and
          of the same class) will be overwritten, otherwise not.

conflict: If a method already exists with the same name (and of the
          same class), different actions can be taken. If '"error"', an
          exception will be thrown and the method will not be created.
          If '"warning"', a 'warning' will be given and the method
          _will_ be created, otherwise the conflict will be passed
          unnotice.

createGeneric: If 'TRUE', a generic S3/UseMethod function is defined
          for this method.

enforceRCC: If 'TRUE', only class names following the R Coding
          Convention is accepted. If the RCC is violated an
          RccViolationException is thrown.

appendVarArgs: If 'TRUE', argument '...' is added with a warning, if
          missing.  For special methods such as '$' and '[[', this is
          never done. This will increase the chances that the method is
          consistent with a generic function with many arguments and/or
          argument '...'.

     ...: Not used.

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

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

_S_e_e _A_l_s_o:

     To define a class, see 'setConstructorS3'(). For a thorough
     example of how to use this method see 'Object'. For information
     about the R Coding Conventions, see 'RccViolationException'. For
     more information about S3/UseMethod, see 'UseMethod'().

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

      ######################################################################
     # Example 1
     ######################################################################
     setMethodS3("foo", "default", function(x, ...) {
       cat("In default foo():\n");
       print(x, ...);
     })

     setMethodS3("foo", "character", function(s, ...) {
       cat("In foo() for class 'character':\n");
       print(s, ...);
     })

     # The generic function is automatically created!
     print(foo)

     foo(123)
     foo("123")

     ######################################################################
     # Example 2
     #
     # Assume that in a loaded package there is already a function bar(),
     # but you also want to use the name 'bar' for the character string.
     # It may even be the case that you do not know of the other package,
     # but your users do!
     ######################################################################
     # bar() in other package
     bar <- function(x, y, ...) {
       cat("In bar() of 'other' package.\n");
     }

     # Your defintion; will redefine bar() above to bar.default().
     setMethodS3("bar", "character", function(object, ...) {
       cat("In bar() for class 'character':\n");
       print(object, ...);
     })

     bar(123)
     bar("123")



      ## Not run: For a complete example see help(Object).

