[Rd] can we override "if" in R?
csardi.gabor at gmail.com
Sat Mar 4 19:22:35 CET 2017
`!` is a generic, `if` is not. You can define an `if` that is generic,
but this might be even more dangerous....
❯ `if` <- function(a, b, c) UseMethod("if")
❯ `if.default` <- function(a,b,c) base::`if`(a, b, c)
❯ `if.foo` <- function(a, b, c) FALSE
❯ a <- structure(42, class = "foo")
❯ if (a) TRUE else FALSE
❯ if (1) TRUE else FALSE
On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote:
> Can I override it for a specific class?
> I can do that for operators such as "!". For example, "!.fm" works for
> objects of the class "fm".
> It seems I can't do the same for "if".
> On Sat, Mar 4, 2017 at 12:41 PM, Gábor Csárdi <csardi.gabor at gmail.com> wrote:
>> You can. Perhaps needless to say, be careful with this.
>> ❯ `if` <- function(...) FALSE
>> ❯ if (TRUE) TRUE else FALSE
>>  FALSE
>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> wrote:
>>> I heard we can override almost everything in R. Is it possible to
>>> override "if" keyword in R to evaluate my own object instead of a
>>> logical value?
>>> R-devel at r-project.org mailing list
More information about the R-devel