From g@bembecker @end|ng |rom gm@||@com  Tue Mar 26 05:58:37 2019
From: g@bembecker @end|ng |rom gm@||@com (Gabriel Becker)
Date: Mon, 25 Mar 2019 21:58:37 -0700
Subject: [Rd] bugs in head() and tail()
In-Reply-To: <CAB8pepwajebtmzuGyMzd7ak_PhMakSwkXS-JEqV4VFW_2kh7sw@mail.gmail.com>
References: <CAB8pepwajebtmzuGyMzd7ak_PhMakSwkXS-JEqV4VFW_2kh7sw@mail.gmail.com>
Message-ID: <CAD4oTHFbbRs5GvhyR33+0mvXmsHFsBAK8hL_2CBDC_eeEA0e_w@mail.gmail.com>

Hi Abs,

This is because the class is "f", not c("f", "function") in your second
example. S3 method dispatch is doing what you (unintentionally, I presume)
asked it to do.

The S3 method which allows head to take functions is utils:::head.function.
S3 can only be expected to  understand inheritance if the value of class(f)
is multivalued. It doesn't have any way of knowing  f is still a function
after the class assignment because "function" does not appear anywhere in
the class vector, so instead of hitting utils:::head.function, it hits
utils:::head.default, which uses [ on the argument, causing the error.

I'd say this is "expected" behavior within the context of the S3 system.

I also see this behavior at least as far aback as 3.5.1, so its not new to
3.5.3.

Best,
~G

On Mon, Mar 25, 2019 at 8:44 PM Abs Spurdle <spurdle.a at gmail.com> wrote:

> (Using R 3.5.3).
>
> I found bugs in head() and tail().
>
> The following works:
>
> > f = function () 1
> > head (f)
> 1 function ()
> 2 1
>
> However, the following does not:
>
> > class (f) = "f"
> > head (f)
> Error in x[seq_len(n)] : object of type 'closure' is not subsettable
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] bugs in head() and tail()
Gabriel Becker