From b@row||ng@on @end|ng |rom gm@||@com  Wed Mar 27 09:28:04 2019
From: b@row||ng@on @end|ng |rom gm@||@com (Barry Rowlingson)
Date: Wed, 27 Mar 2019 08:28:04 +0000
Subject: [Rd] bugs in head() and tail()
In-Reply-To: <c98362642aea41abb3c43090bf5693b2@CWXP265MB0775.GBRP265.PROD.OUTLOOK.COM>
References: <CAB8pepwajebtmzuGyMzd7ak_PhMakSwkXS-JEqV4VFW_2kh7sw@mail.gmail.com>
 <CAD4oTHFbbRs5GvhyR33+0mvXmsHFsBAK8hL_2CBDC_eeEA0e_w@mail.gmail.com>
 <c98362642aea41abb3c43090bf5693b2@CWXP265MB0775.GBRP265.PROD.OUTLOOK.COM>
Message-ID: <CANVKczMFJyZwm7LPiEnYk1mtU6_rzx0L4SeCbhVmHz+KBTLRNA@mail.gmail.com>

On Wed, Mar 27, 2019 at 1:52 AM Abs Spurdle <spurdle.a at 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.

Barry





>         [[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()
Barry Rowlingson