From tlumley at u.washington.edu  Thu Apr  6 22:44:50 2006
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Thu, 6 Apr 2006 13:44:50 -0700 (PDT)
Subject: [Rd] prevent reassignment of function names
In-Reply-To: <44357024.6090804@metrumrg.com>
References: <44357024.6090804@metrumrg.com>
Message-ID: <Pine.LNX.4.64.0604061331020.19619@homer24.u.washington.edu>

On Thu, 6 Apr 2006, Tim Bergsma wrote:

> Hi.
>
> I'm trying to find a systematic way to prevent assignment to names of
> existing functions.  I've tried reassigning to the assignment operator,
> with mixed results.  The function definition for "<-" below works as
> hoped for the demonstrated assignments to a and c.  However, for the
> assignment based on the test function, it appears that the formal
> argument is not visible.  Any suggestions?
>

Well, my first suggestion would be not to do it. I don't see any real 
benefit, and messing around with something as basic as assignment is 
likely to break something.  If nothing else, there is certainly code out 
there where people have, say, "df" or "c" as a variable name, and it isn't 
doing any harm.

As a programming exercise it seems that the following works

"<-" <- function(x,value){
         if(tryCatch(is.function(x),error=function(e) FALSE))
                 stop("assignment to an existing function")
         else  eval.parent(substitute(.Primitive("<-")(x,value)))
}


 	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



[Rd] prevent reassignment of function names
Thomas Lumley