From luke-tier@ey m@ili@g off uiow@@edu  Wed Sep  5 23:38:38 2018
From: luke-tier@ey m@ili@g off uiow@@edu (luke-tier@ey m@ili@g off uiow@@edu)
Date: Wed, 5 Sep 2018 16:38:38 -0500 (CDT)
Subject: [Rd] True length - length(unclass(x)) - without having to call
 unclass()?
In-Reply-To: <CAJXgQP3QkqEEWoCsw7cyHB3-XNL3h-z7-v20tODjf9bpVium2g@mail.gmail.com>
References: <CAFDcVCQdmLYM4gPj_zoz4KeoHGGUzJux9hLK_3vMVaeWUsrHQQ@mail.gmail.com>
 <d16444c2-a703-d822-bb59-f94c92ecb260@gmail.com>
 <CALEXWq15ASNPhOOd0PiF0F2ky09j-QNk6=qWTfoK3ZEqTBAjYg@mail.gmail.com>
 <CAJXgQP3QkqEEWoCsw7cyHB3-XNL3h-z7-v20tODjf9bpVium2g@mail.gmail.com>
Message-ID: <alpine.DEB.2.21.1809051634260.2971@luke-Latitude-7480>

On Wed, 5 Sep 2018, Kevin Ushey wrote:

> More generally, I think one of the issues is that R is not yet able to
> decrement a reference count (or mark a 'shared' data object as
> 'unshared' after it knows only one binding to it exists). This means
> passing variables to R closures will mark that object as shared:
>
>    x <- list()
>    .Internal(inspect(x))  # NAM(1)
>    identity(x)
>    .Internal(inspect(x))  # NAM(3)
>
> I think for this reason users often resort to 'hacks' that involve
> directly setting attributes on the object, since they 'know' only one
> reference to a particular object exists. I'm not sure if this really
> is 'safe', though -- likely not given potential future optimizations
> to R, as Tomas has alluded to.
>
> I think true reference counting has been implemented in the R sources,
> but the switch has not yet been flipped to enable that by default.
> Hopefully having that will make cases like the above work as expected?

Current R-devel built with reference counting by setting

CFLAGS="-O3 -g -Wall -pedantic -DSWITCH_TO_REFCNT"

gives


x <- list()
.Internal(inspect(x))
## @55ad788e3b28 19 VECSXP g0c0 [REF(1)] (len=0, tl=0)
identity(x)
## list()
.Internal(inspect(x))
## @55ad788e3b28 19 VECSXP g0c0 [REF(1)] (len=0, tl=0)

I'm moderately hopeful we'll be able to switch to this for 3.6.0 but
depends on finding enough time to sort out some loose ends.

Best,

luke

>
> Thanks,
> Kevin
>
> On Wed, Sep 5, 2018 at 2:19 AM I?aki Ucar <iucar at fedoraproject.org> wrote:
>>
>> The bottomline here is that one can always call a base method,
>> inexpensively and without modifying the object, in, let's say,
>> *formal* OOP languages. In R, this is not possible in general. It
>> would be possible if there was always a foo.default, but primitives
>> use internal dispatch.
>>
>> I was wondering whether it would be possible to provide a super(x, n)
>> function which simply causes the dispatching system to avoid "n"
>> classes in the hierarchy, so that:
>>
>>> x <- structure(list(), class=c("foo", "bar"))
>>> length(super(x, 0)) # looks for a length.foo
>>> length(super(x, 1)) # looks for a length.bar
>>> length(super(x, 2)) # calls the default
>>> length(super(x, Inf)) # calls the default
>>
>> I?aki
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


[Rd] True length - length(unclass(x)) - without having to call unclass()?
luke-tier@ey m@ili@g off uiow@@edu