hothorn@ci.tuwien.ac.at
2000-Mar-03 09:10 UTC
[Rd] using function names as parameters (PR#469)
When using a function name as parameter, here date, the following happends: R> foo <- function(date=date()) cat(date, "\n") R> foo() Error in cat(date, "\n") : recursive default argument reference R> foo("Good Morning!") Good Morning! works. Torsten --please do not edit the information below-- Version: platform = i686-unknown-linux arch = i686 os = linux system = i686, linux status = major = 1 minor = 0.0 year = 2000 month = February day = 29 language = R Search Path: .GlobalEnv, Autoloads, package:base -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
hothorn@ci.tuwien.ac.at writes:> When using a function name as parameter, here date, the following happends: > > R> foo <- function(date=date()) cat(date, "\n") > R> foo() > Error in cat(date, "\n") : recursive default argument referenceThis is not a bug. Default arguments are evaluated in the frame of the called function, so when the date variable is requested and no actual argument was supplied, R tries to evaluate the default expression, but that involved the variable "date" again. If you pass an argument, the default expression is never evaluated and the problem does not arise. Similarly a definition like function(x=y, y=x) x + y is asking for trouble if both x and y are left unspecified on the call. [This has nothing to do with passing functions as parameters, by the way] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Friedrich.Leisch@ci.tuwien.ac.at
2000-Mar-03 12:10 UTC
[Rd] using function names as parameters (PR#469)
>>>>> On 03 Mar 2000 12:55:21 +0100, >>>>> Peter Dalgaard BSA (PDB) wrote:PDB> hothorn@ci.tuwien.ac.at writes:>> When using a function name as parameter, here date, the following happends: >>R> foo <- function(date=date()) cat(date, "\n") R> foo()>> Error in cat(date, "\n") : recursive default argument referencePDB> This is not a bug. Default arguments are evaluated in the frame of the PDB> called function, so when the date variable is requested and no actual PDB> argument was supplied, R tries to evaluate the default expression, but PDB> that involved the variable "date" again. If you pass an argument, the PDB> default expression is never evaluated and the problem does not arise. PDB> Similarly a definition like PDB> function(x=y, y=x) x + y PDB> is asking for trouble if both x and y are left unspecified on the call. PDB> [This has nothing to do with passing functions as parameters, by the PDB> way] [Taken to r-core] Hmm, I asked Torsten to mail the bug report ... I first thought along your lines and I would agree if I pass ``date'' as default, but we have ``date()'' as default argument which is the return value of the function, no? IMO evaluating the default expression comes down to evaluating ``date <- date()'' which is perfectly legal ... BTW, BB can do it: S> foo <- function(date=date()) cat(date, "\n") S> foo() Fri Mar 3 09:49:20 MET 2000 Warning messages: looking for function "date", ignored local non-function in: foo() .f -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
p.dalgaard@biostat.ku.dk
2000-Mar-03 13:26 UTC
[Rd] using function names as parameters (PR#469)
Friedrich Leisch <Friedrich.Leisch@ci.tuwien.ac.at> writes:> Hmm, I asked Torsten to mail the bug report ... I first thought along > your lines and I would agree if I pass ``date'' as default, but we > have ``date()'' as default argument which is the return value of the > function, no? > > IMO evaluating the default expression comes down to evaluating > ``date <- date()'' which is perfectly legal ... > > BTW, BB can do it: > > S> foo <- function(date=date()) cat(date, "\n") > S> foo() > Fri Mar 3 09:49:20 MET 2000 > Warning messages: > looking for function "date", ignored local non-function in: foo()However,> foo <- function(date=date) date > foo()Error in foo(): Recursive occurrence of default argument "date" Dumped So S-plus only works by assuming that the local variable is not a function (*before* evaluating its default)! R is being cleaner and more consistent.. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._