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

Barry Rowlingson b@row||ng@on @end|ng |rom gm@||@com
Wed Mar 27 09:28:04 CET 2019

On Wed, Mar 27, 2019 at 1:52 AM Abs Spurdle <spurdle.a using gmail.com> wrote:

> In the case of head.default(), it assumes that the object is a vector, or
> something similar.

No it doesn't. It assumes (ultimately) that x[seq_len(n)] is the correct
way to generate a "head" of something. Which is reasonable. That's
dependent on the implementation of the `[` method on object `x`.

> Resulting in the error above, which fails to recognize that the input was
> unsuitable.
> Because the object you fed it didn't have a method for `head` or for `[`.
Its the object designer's responsibility to do that. And by creating a
function that doesn't have the "function" class - that means *you*.

There's no way that head.default can inspect everything it might get fed,
including classes that have yet to be made, to see if it can do anything
meaningful with it. So it uses what looks like a reasonable approach -
apply `[` on the object with the first `n` elements. Let the class decide
what to do with it.

You might think generics should trap errors and give meaningful errors, but
the error isn't in the generic here - its in the `[` method of the class it
was fed. `head` doesn't know or care about that - again, its the class
designers responsibility to handle that, and the users responsibility to
*not* call generics on objects for which there's no meaningful behaviour.
It's really the error message of `[.default` seeming obscure to you that is
your issue here. If it said "cannot create subsets of this object with ["
would that please you? Again, that's not a bug in `head` or anything to do
with method dispatch.

So lets take a look at your choices here:

 "If a generic has a default method, then that default method should be
guaranteed to work."

 - as demonstrated, the default here does its job. it does "work".

"Or at least, provide a useful error message, that makes it obvious to the
user, what he or she has done wrong."

 and that error message is the responsibility of methods of the object, in
this case `[`.

therefore its not a bug.


>         [[alternative HTML version deleted]]
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

	[[alternative HTML version deleted]]

More information about the R-devel mailing list