From kevinu@hey @ending from gm@il@com  Wed Sep  5 18:30:34 2018
From: kevinu@hey @ending from gm@il@com (Kevin Ushey)
Date: Wed, 5 Sep 2018 09:30:34 -0700
Subject: [Rd] True length - length(unclass(x)) - without having to call
 unclass()?
In-Reply-To: <CALEXWq15ASNPhOOd0PiF0F2ky09j-QNk6=qWTfoK3ZEqTBAjYg@mail.gmail.com>
References: <CAFDcVCQdmLYM4gPj_zoz4KeoHGGUzJux9hLK_3vMVaeWUsrHQQ@mail.gmail.com>
 <d16444c2-a703-d822-bb59-f94c92ecb260@gmail.com>
 <CALEXWq15ASNPhOOd0PiF0F2ky09j-QNk6=qWTfoK3ZEqTBAjYg@mail.gmail.com>
Message-ID: <CAJXgQP3QkqEEWoCsw7cyHB3-XNL3h-z7-v20tODjf9bpVium2g@mail.gmail.com>

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?

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
>



[Rd] True length - length(unclass(x)) - without having to call unclass()?
Kevin Ushey