From jmc at research.bell-labs.com  Tue Apr  1 10:23:42 2003
From: jmc at research.bell-labs.com (John Chambers)
Date: Tue Apr  1 16:24:24 2003
Subject: [Rd] setGeneric
References: <Pine.SOL.4.30.0304010505550.24055-100000@ysidro.econ.uiuc.edu>
Message-ID: <3E89A0EE.4036A4C5@research.bell-labs.com>

I think this is a consequence of the extra context added to make methods
work right with R lexical scoping, namespaces, etc.  Or a subtlety in
R's definition of missing()?

The problem is that somehow the default expression for argument `ncol'
makes that argument appear NOT to be missing.  But an attempt to
evaluate it fails because the local variable `n' hasn't been defined
yet.

The following debugging snippets, using trace on the method for
"numeric", show what's happening.  But as to why, we need some expert
help! (Luke?)

R> trace("diag", sig="numeric", browser)
[1] "diag"
R> diag(2)
Tracing diag(2) on entry 
Called from: diag(2)
Browse[1]> missing(x)
[1] FALSE
Browse[1]> missing(nrow)
[1] TRUE
Browse[1]> missing(ncol)
[1] FALSE
Browse[1]> ncol
Error during wrapup: Object "n" not found

R> selectMethod("diag", "numeric")
Method Definition (Class "MethodDefinitionWithTrace"):

function (x = 1, nrow, ncol = n) 
{
......

By the way, while this example seems to be some obscure glitch, there is
a real potential for code that relies heavily on lazy evaluation to
break when functions are made generic, just for the simple reason that
arguments involved in method signatures (ANY defined method signature)
may need to be evaluated to dispatch the method, before they would
normally be needed in the body of the original non-generic function.

John

Roger Koenker wrote:
> 
> I'm still having difficulties with methods...is the following behavior expected?
> (This is without loading any libraries)
> 
> R : Copyright 2003, The R Development Core Team
> Version 1.7.0 Under development (unstable) (2003-03-31)
> 
> > ls()
> character(0)
> >
> > diag(2)
>      [,1] [,2]
> [1,]    1    0
> [2,]    0    1
> > setGeneric("diag")
> [1] "diag"
> > diag(2)
> Error in diag(2) : Object "n" not found
> >
> 
> Seems to be a perverse consequence of some slightly fancy use of lazy evaluation....
> 
> url:    www.econ.uiuc.edu       Roger Koenker           Dept. of Economics UCL,
> email   rkoenker@uiuc.edu       Department of Economics Drayton House,
> vox:    217-333-4558            University of Illinois  30 Gorden St,
> fax:    217-244-6678            Champaign, IL 61820     London,WC1H 0AX, UK
>                                                         vox:    020-7679-5838
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc@bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc


[Rd] setGeneric
John Chambers