R unary operator overload: risks?

前端 未结 2 1581
星月不相逢
星月不相逢 2021-02-05 09:18

In my continuing quest to avoid using parentheses for some simple commands, I wrote up the following operator to create a new graphics window. My question is: am I at risk of \

2条回答
  •  隐瞒了意图╮
    2021-02-05 09:47

    I executed "!" = function(a){stop("'NOT' is used")} and executed the replications function, which uses the ! operator, and this worked fine. So it looks like it is safe to override "!".

    Still you probably want to use classes, which you can do as follows:

    # Create your object and set the class
    A = 42
    class(A) = c("my_class")
    
    # override ! for my_class
    "!.my_class" = function(v){ 
      cat("Do wathever you want here. Argument =",v,"\n")
    }
    
    # Test ! on A
    !A
    

提交回复
热议问题