From Bill.Venables at csiro.au  Wed May 18 03:42:01 2011
From: Bill.Venables at csiro.au (Bill.Venables at csiro.au)
Date: Wed, 18 May 2011 11:42:01 +1000
Subject: [Rd] S3 method dispatch - can methods come from the enclosing
 environment?
In-Reply-To: <AA244BFB-BC4E-486D-B15E-DB30807D5668@r-project.org>
References: <BANLkTi=zA1s+DwgN+0qhf7_4CkZ-VBPY9A@mail.gmail.com>
	<BANLkTim0BpLDYtU=FebEriF7Q_AMmToGbA@mail.gmail.com>
	<4DCDA3ED.4050607@gmail.com>
	<BANLkTi=4ewhn1fm5EKS5hZ=pFtfgFT0PGw@mail.gmail.com>
	<4DD253D9.5070108@gmail.com>
	<1BDAE2969943D540934EE8B4EF68F95FE49C204ED8@EXNSW-MBX03.nexus.csiro.au>
	<AA244BFB-BC4E-486D-B15E-DB30807D5668@r-project.org>
Message-ID: <1BDAE2969943D540934EE8B4EF68F95FE49C204EDA@EXNSW-MBX03.nexus.csiro.au>

My apologies...  how embarrassing.

Please resume your normal duties.  :-)

Bill. 

-----Original Message-----
From: Simon Urbanek [mailto:simon.urbanek at r-project.org] 
Sent: Wednesday, 18 May 2011 10:36 AM
To: Venables, Bill (CMIS, Dutton Park)
Cc: r-devel at r-project.org
Subject: Re: [Rd] S3 method dispatch - can methods come from the enclosing environment?

Bill,

I suspect you have a typo there [class(x) instead of class(z)] which is why it doesn't work. Without the typo it does:

> tstFn <- local({
+   print.tst <- function(x, ...) cat("found it!")
+ 
+   function(x) print(x)
+ })
> 
> z <- "The cat sat on the mat."
> class(z) <- "tst"
> 
> tstFn(z)
found it!

Cheers,
Simon


On May 17, 2011, at 7:32 PM, <Bill.Venables at csiro.au> <Bill.Venables at csiro.au> wrote:

> I was surprised to see that S3 methods are not found if they only reside in the enclosing environment.  E.g.:
> 
>> tstFn <- local({
> +   print.tst <- function(x, ...) cat("found it!")
> + 
> +   function(x) print(x)
> + })
>> 
>> z <- "The cat sat on the mat."
>> class(x) <- "tst"
>> 
>> tstFn(z)
> [1] "The cat sat on the mat."
>> 
> 
> So the answer to the question posed in the subject line is apparently "no". Perhaps this is well known and fully documented somewhere, but if so it has eluded me.  Is there some reason for this?  
> 
> I'm not sure if this is a bug, a feature, or a proposal for development, but I would welcome people's views on this curious little issue.
> 
> Bill Venables.
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] S3 method dispatch - can methods come from the enclosing environment?
Bill.Venables at csiro.au