From pdalgd at gmail.com  Tue Aug 12 09:28:05 2014
From: pdalgd at gmail.com (peter dalgaard)
Date: Tue, 12 Aug 2014 09:28:05 +0200
Subject: [Rd] Define 'in' for new class
In-Reply-To: <CAA+5f=1yhAbDL9ndk75A88UrBujFZ1DdfY7Q0AO1OODH6-zeZA@mail.gmail.com>
References: <CAA+5f=1yhAbDL9ndk75A88UrBujFZ1DdfY7Q0AO1OODH6-zeZA@mail.gmail.com>
Message-ID: <4C06649A-38B1-47B3-9B7D-F5F0D2393E16@gmail.com>


On 12 Aug 2014, at 08:29 , Simon Knapp <sleepingwell at gmail.com> wrote:

> Hi List,
> 
> Is it possible to define how 'in' works for an object of a specific class
> (to achieve a similar result to implementing the iterator protocol in a
> class Python)?
> 

No, because 'in' doesn't really exist, it is "syntactic sugar" to sweeten a call to the "for" function with its three arguments. E.g.

> `for`(i,1:2,print(i))
[1] 1
[1] 2

So what you're really asking for is something like a `for` with class dispatch or the two first arguments replaced by an iterator mechanism. That's not in the cards, at least at the moment, but I suppose it isn't completely out of the question either -- ideas of this sort get bandied around occasionally. It would require changes to R's internals; it is not something you can do in (say) a package.


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

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Define 'in' for new class
peter dalgaard