HenrikBengtsson / R.oo

R package: R.oo - R Object-Oriented Programming with or without References
https://cran.r-project.org/package=R.oo
20 stars 1 forks source link

Add exact=TRUE to `[[()` #15

Closed HenrikBengtsson closed 8 years ago

HenrikBengtsson commented 8 years ago

R's [[() gained argument exact = TRUE a while ago (not sure in which version). For example:

> x <- list(abc=1)
> x[["abc", exact=TRUE]]
[1] 1
> x[["ab", exact=TRUE]]
NULL
> x[["ab", exact=FALSE]]
[1] 1

Action

Add an exact argument for [[.Object as well, or at least ... such that it doesn't give an error if it is passed.

HenrikBengtsson commented 8 years ago

One reason is for instance if someone uses getElement() on an Object, cf.

> getElement
function (object, name) 
{
    if (isS4(object)) 
        methods::slot(object, name)
    else object[[name, exact = TRUE]]
}
<bytecode: 0x2349a08>
<environment: namespace:base>

which gives an error with R.oo (<= 1.20.0);

> library("R.oo")
> obj <- Object()
> obj$abc <- 1
> getElement(obj, "abc")
Error in `[[.Object`(object, name, exact = TRUE) : 
  unused argument (exact = TRUE)
HenrikBengtsson commented 8 years ago

The exact argument goes back to at least R 2.11.0 and since R.oo requires R (>= 2.13.0) we can safely add it to the package without having to worry about backward compatibility.

HenrikBengtsson commented 8 years ago

Added to develop branch; the following now works:

> library("R.oo")
> obj <- Object()
> obj$abc <- 1
> getElement(obj, "abc")
1
> getElement(obj, "abcd")
NULL
> obj[["abc"]]
[1] 1
> obj[["abc", exact=TRUE]]

However, argument exact is currently ignored, e.g.

> obj[["abc", exact=FALSE]]
[1] 1
> obj[["ab", exact=FALSE]]
NULL
HenrikBengtsson commented 8 years ago

This required:

o Now $() for Object and Class calls [[() for ditto (was vice versa).

which may cause problems downstreams, e.g. infinite recursive call loop;

22: getFile.GenericDataFileSet(x, i, ...)
21: getFile(x, i, ...)
20: `[[.GenericDataFileSet`(this, files)
19: `$.Object`(this, files)
18: this$files
17: getFile.GenericDataFileSet(x, i, ...)
16: getFile(x, i, ...)
15: `[[.GenericDataFileSet`(this, files)
14: `$.Object`(this, files)
13: this$files
12: getFiles.GenericDataFileSet(this)
11: getFiles(this)

Thus, more care needs to be put into this.