Binding: Ruby-like Binding

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/Binding.R

Description

Ruby-like binding to help mimicking OOP in the ruby system.

Usage

1
2
3
4
Binding(env,self=FALSE)
binding$.var <- value
binding$.var
binding$var

Arguments

env

an environment.

binding

an object of class Binding.

var

a variable name.

value

any R object or an object of class "ptR".

Details

Let us notice that the class Binding is mainly proposed for development use. Thanks to the environment object provided by the R system, it is possible to mimick the basic principles of usual Oriented Object Programmation used in several languages as Ruby, Python, .... The concept of encapsulation is provided by the class Binding. An object, called binding for example, of the class Binding is no more than an environment with extraction facilities. Inside the object binding, objects named <name>.ptR are considered as dynamic (i.e., not static) fields when objects named <name>.RMethod are considered as methods. A dynamic field is simply an object of class ptR (often called pointer). A method is no more than a function with object binding as environment. The class Binding allows us to manipulate pointers (i.e., objects of class ptR) and methods in a transparent way. binding$.pt <- value creates a pointer pt actually saved in the object pt.ptR inside the environment binding. binding$.pt returns the ptR object (saved in pt.ptR) and binding$pt directly returns the value of the ptR object (inside pt.ptR). There is currently no explicit way to assign method in a Binding object. A method meth is simply declared inside binding by typing code of the form binding$meth.RMethod <- function(...) {...}. Extraction is however specifically supplied by binding$meth (i.e. without code.RMethod). In such a way, the environment binding is applied to the automatically extracted function meth.RMethod which then acts as a method. Any other extraction of the form binding$name when no object called name.ptR or name.RMethod is then considered as a regular (static) R object inside the environment binding. One then understand that the function meth.RMethod is then considered as a regular function and not a method.

The option self=TRUE when calling Binding completes the creation of the environment by assigning the object self inside the newly created environment to the environment itself. Combined with the extraction facilities provided by the class Binding, this allow the user to develop methods using pointers encapsulated inside the environment (see the example below).

Value

Binding returns an object of class Binding. When called with self=TRUE, the resulting object (which is an environment) contains the object self with value the environment itself.

Author(s)

R. Drouilhet

See Also

ptR, CqlsObj, RObj

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"nom" %<-% "Brown" # equivalent to nom <- ptR("Brown")
nom
nom$value

# new Binding which is simply an environment of class "Binding"
b <- Binding()
b$.nom <- "toto" # nom.ptR is created in binding b with value "toto"
b$.nom           # equivalent to b$nom.ptR
b$nom            # equivalent to b$.nom$value or b$nom.ptR$value

#dynamic stuff
b$.nom <- nom
b$.nom
nom %<-% "Bond"
b$.nom

# method mechanism
b2 <- Binding(self=TRUE) # self is created inside the environment b2 with value b2 
b2$.nom <- "James Bond"
b2
# method hello is created by specifically appending ".RMethod" 
b2$hello.RMethod <- function() cat("I am ",self$nom,"!\n",sep="")
# Call the method as below
b2$hello()        # => as a method, its environment is b2 itself!
# Be carefull, compare the environments of the two following functions
b2$hello
b2$hello.RMethod
# Indeed, the following call is not a method call! 
# b2$hello.RMethod()  # => error since its environment is not b2 and self is then unknown.

rcqls/CqlsRObj documentation built on May 27, 2019, 3:04 a.m.