Re: [Rd] Defining a method that behaves like '$'?

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Sat, 10 Jul 2010 13:24:36 +0100

On Fri, Jul 9, 2010 at 2:10 PM, Renaud Gaujoux <renaud_at_mancala.cbio.uct.ac.za> wrote:
> I do not want to access the slot itself but its content: a:toto would be
> a_at_slot1[['toto']].
> The thing is that I would like to have two different methods: '$' (that I
> already have) and another one to define, ideally that behaves like '$'.
> So in brief:
> - a:toto would be for a_at_slot1[['toto']]
> - a$tata would be for a_at_slot2[['tata']]
>
> But apparently it might not be possible.
>

 Even if possible, definitely not desirable. As already mentioned, a:b is the sequence a to b (as in 0:10), so it's going to look weird to anyone who hasn't noticed your definition. Also, it looks fairly meaningless. By which I mean there's no obvious reason why a colon should do what you want it to do. There's also no obvious reason why a dollar sign does what it does (whats it got to do with dollars?) but we've had it for 20 years so we're stuck with it.

 Write a method for your objects and force your users to do a bit more typing as a trade-off for legibility:

 slot1(a,"toto")

 is a lot more readable than a:toto (assuming you replace 'slot1' with something meaningful).

 Remember, code is most likely to be written once, and read many times - so make it easy for readers!

Barry



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 10 Jul 2010 - 12:30:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 10 Jul 2010 - 17:40:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive