wch/R6 — pull request #34 by jeroenooms (2014-10-26 00:30:18+00:00)
Title: Return error instead of NULL when accessing non-existing fields.

One thing that I currently prefer about reference classes is that an error is raised when trying to access a non existing variable. For example the `greet` method below contains a typo (`nane` instead of `name`) which results in the appropriate error message.

```r
Person <- setRefClass("Person",
fields = list(
name = "character"
),
methods = list(
greet = function() {
cat("Hello, my name is ", nane, ".\n")
}
)
)

p2 <- Person(name = "jerry")
p2$greet()
# Error in cat("Hello, my name is ", nane, ".\n") : object 'nane' not found
````

Similar for non existing methods:

``` r
p2$bla();
# Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
# ‘bla’ is not a valid field or method name for reference class “Person”
```

This error is unambiguous and easy to debug. However in R6, non existing variables are `NULL`.

``` r
Person <- R6Class("Person",
public = list(
name = NA,
greet = function() {
cat("Hello, my name is ", self$nane, ".\n")
}
)
)

p1 <- Person$new()
p1$name <- "jerry"
p1$greet()
# Hello, my name is .
p1$bla()
# Error: attempt to apply non-function
```

IMO, this makes R6 classes more error prone and harder to debug. The current pull request will raise an error when a non existing field in `self` is accessed. For example:

``` r
Person <- R6Class("Person",
public = list(
name = NA,
greet = function() {
cat("Hello, my name is ", self$nane, ".\n")
}
)
)

p1 <- Person$new()
p1$foo
# Error: R6 class 'Person' has no public field 'foo'.

p1$name
NA

p1$greet()
# Error: R6 class 'Person' has no public field 'nane'.
```