[Rd] Define 'in' for new class

peter dalgaard pdalgd at gmail.com
Tue Aug 12 09:28:05 CEST 2014


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



More information about the R-devel mailing list