From b.rowe at baml.com  Fri Sep 24 00:44:06 2010
From: b.rowe at baml.com (Rowe, Brian - Eqty NY)
Date: Thu, 23 Sep 2010 18:44:06 -0400
Subject: [Rd] Behavior of R CMD build and library() w.r.t. setGeneric-like
	functions
Message-ID: <CD52DB5226F62F4EBC1116CD5E9BE898021B14DA@ex2k.bankofamerica.com>

Hello developeRs,

Apologies in advance for a rather long email, but to describe the
problem, I need to step through many details. I have been working on a
new dispatching system (futile.paradigm on CRAN) based on functional
programming concepts that is an alternative to S3 and S4 dispatching. I
use a declarative syntax using guard statements to control the
dispatching between function variants. I also provide post assertions
via an 'ensure' command that programatically behave similar to the
'guard' command. In a sense, these work like setGeneric/setMethod in S4.

As a simple example, I write code like this:
  guard(month.date, isa(Date,date))
  ensure(month.date, as.numeric(result) < 13)
  month.date <- function(date) format(date, '%m')

  guard(month.int, is.numeric(date))
  month.int <- function(date) month(i2date(date))

and call it like this:
  > month(20100913)
  [1] "09"

Behind the scenes, the guard command is using 'assign' to create a
parent function with the name 'month'. This can be done explicitly as
well, although it is typically not necessary. The parent definition
looks like this:
  month <- function(...) UseFunction('month',...)

The package works fine when I'm writing scripts, but all heck breaks
loose when I try to write a package that depends on futile.paradigm.
When I run R CMD check, it doesn't seem that the guard commands are
being executed. A less likely hypothesis is that they are being deleted
by the cleanEx() function.

* checking examples ... ERROR
Running examples in 'pars.core-Ex.R' failed.
The error most likely occurred in:
>   set_date(20100921)
Error: could not find function "set_date"
Execution halted

If I add the explicit function definition for 'month' as shown above, I
get one of my own error messages indicating that no guard statements
were executed.

>   set_date(20100921)
Error in UseFunction("set_date", ...) :
  Function must have guards for functional dispatching
Calls: set_date -> UseFunction
Execution halted

I decided to bypass the check process and just install the built package
to see if it is an issue with check as opposed to the package.
Unfortunately, it seems that when the package is built, the guard
commands get stripped from the package.

> library(futile.paradigm)
Loading required package: futile.options
> debug(guard)
> library(pars.core)
Loading required package: futile.logger 

It's unclear to me why this happens when setClass, setGeneric, etc work
under seemingly similar conditions. Is this because of the export*
declarations in the NAMESPACE? What happens in the R CMD build process
that strips these function calls and their side-effects? Finally, what
do I need to do to make my 'guard' and 'ensure' commands operate
properly in a package context?

Warm Regards,
Brian Rowe

----------------------------------------------------------------------
This message w/attachments (message) is intended solely ...{{dropped:7}}



[Rd] Behavior of R CMD build and library() w.r.t. setGeneric-like functions
Rowe, Brian - Eqty NY