Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R/二进制运算符重载中的简单OOP系统_R_Oop - Fatal编程技术网

R/二进制运算符重载中的简单OOP系统

R/二进制运算符重载中的简单OOP系统,r,oop,R,Oop,我正在探索R的功能,并试图构建一个非常简单的面向对象编程系统 其思想是使用函数环境来存储对象属性,并重载“>”操作符以替换OOP语言中通常使用的“.”。这将在下面实现 `>.object` <- function (this, variable) { return(eval(substitute(variable), environment(this))) } new <- function (obj) { structure(unclass(obj), class=

我正在探索R的功能,并试图构建一个非常简单的面向对象编程系统

其思想是使用函数环境来存储对象属性,并重载“>”操作符以替换OOP语言中通常使用的“.”。这将在下面实现

`>.object` <- function (this, variable) {
  return(eval(substitute(variable), environment(this)))
}

new <- function (obj) {
  structure(unclass(obj), class="object")
}
但是,当使用运算符
>
内联时,会尝试在全局环境中解析
bark()
,如果未定义,则会产生错误

max>bark() # Error in bark() : could not find function "bark"
如果在全局环境中以某种方式定义了
bark
,那么就没有错误

bark = function(){}
max>bark() # Prints "Woof!"
有没有办法解决这个问题,使
max>bark()
总是返回“Woof!”

bark = function(){}
max>bark() # Prints "Woof!"