From csardi.gabor at gmail.com  Sun Mar  5 08:13:42 2017
From: csardi.gabor at gmail.com (=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=)
Date: Sun, 5 Mar 2017 07:13:42 +0000
Subject: [Rd] can we override "if" in R?
In-Reply-To: <CAFLer83n1uv_14fLBYg78CkAKiEUsMjeSXObto7gSyS+0TTfpA@mail.gmail.com>
References: <CAFLer83hbY+3QEfoukAmt4Hy77P5=NyW3a+pyigV0CJVjzjYdA@mail.gmail.com>
	<CABtg=KkmsPVZc0Y-Rru5+DkzLHabrkVDOZWcip5jhYHD-D4HZg@mail.gmail.com>
	<CAFLer83OTdZQq+=Uj5g7fqmph8-FWwa7pFSMmz_dhcKhs8DuCA@mail.gmail.com>
	<CABtg=Kn83hAzBGyJkVrWciAYadJDsT2GmLPY4OF2_o30cxG1Ww@mail.gmail.com>
	<CAFLer83n1uv_14fLBYg78CkAKiEUsMjeSXObto7gSyS+0TTfpA@mail.gmail.com>
Message-ID: <CABtg=KmmtEm1m8t6-vngxYEgjLecsRdGHVS62YwCWT21VqXVqA@mail.gmail.com>

Because the S3 class system is very informal. E.g. if you happen to
have an `if.whatever` function, that will be automatically a method of
your generic.

Gabor

On Sat, Mar 4, 2017 at 7:49 PM, Da Zheng <zhengda1936 at gmail.com> wrote:
> I'm just curious. Why making "if" generic is even more dangerous?
>
> Best,
> Da
>
> On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:
>> `!` 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
>> [1] FALSE
>>
>> ? if (1) TRUE else FALSE
>> [1] TRUE
>>
>> Gabor
>>
>> On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote:
>>> Thanks.
>>> 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".
>>>
>>> Best,
>>> Da
>>>
>>> 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
>>>> [1] FALSE
>>>>
>>>> G.
>>>>
>>>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> wrote:
>>>>> Hello,
>>>>>
>>>>> 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?
>>>>>
>>>>> Thanks,
>>>>> Da
>>>>>
>>>>> ______________________________________________
>>>>> R-devel at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] can we override "if" in R?
=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=