R unary operator overload: risks?

前端 未结 2 1582
星月不相逢
星月不相逢 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:39

    with

    makeActiveBinding
    

    you can replace ls() by e.g LS w/o need of unary operators

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题