Le dimanche 21 février 2016 à 08:40 -0800, Tomas Lycken a écrit :
> Thanks, that gets me at least half the way!
>
> However, I still can’t figure out how to give only one warning; if I
> give depwarn the symbols :start, :next and :done for the second
> argument, I get three warnings - if I give it something custom (and
> the same to all) I get three warnings per element. How do I give just
> one warning?
I'd say only call depwarn() from start(). You can't call the other
functions without it, and it's only called once, which will limit the
(significant IIRC) performance penalty of depwarn().
Regards
> // T
>
> On Sunday, February 21, 2016 at 4:53:19 PM UTC+1, Milan Bouchet-Valat wrote:
>
> > Le dimanche 21 février 2016 à 07:30 -0800, Tomas Lycken a écrit :
> > > In a package, I have a method foos which currently returns a
> > > collection of Bars. This is a little unfortunate, so I would like to
> > > instead return a value of type FooCollection on which I can call the
> > > bar function to get the array of Bars. I can get backwards
> > > compatibility by making FooCollection iterable, by implementing
> > > start, next and done. Now, I have the following:
> > >
> > > immutable Bar end
> > > immutable FooCollection
> > > bars::Vector{Bar}
> > > end
> > >
> > > foos() = FooCollection(Bar[])
> > > bars(fc::FooCollection) = fc.bars
> > >
> > > start(fc::FooCollection) = start(fc.bars)
> > > next(fc::FooCollection, state) = next(fc.bars, state)
> > > done(fc::FooCollection, state) = done(fc.bars, state)
> > > This works - both the new and the old api:s are supported, and they
> > > do the right thing.
> > >
> > > Next, I’d like to deprecate iteration on FooCollection, to make it
> > > obvious to users that although their code isn’t broken, they’re
> > > relying on an API that might not work forever. Instead of for bar in
> > > foos() I want them to write for bar in bars(foos()). Thus, I make the
> > > following changes to the iteration protocol:
> > >
> > > @deprecate start(fc::FooCollection) start(bars(fc))
> > > @deprecate next(fc::FooCollection, state) next(bars(fc), state)
> > > @deprecate done(fc::FooCollection, state) done(bars(fc), state)
> > > This works and gives deprecation messages when I write for bar in
> > > foos() println(bar) end, but the messages aren’t very helpful; for
> > > one thing, I get three of them (one for each method), and for another
> > > they refer to functions which aren’t present in the user’s code (and
> > > which the user won’t realize why they’re called unless they know how
> > > to implement iterators in Julia…).
> > >
> > > Is there a better/more helpful way to do this, without having to re-
> > > implement the @deprecate machinery entirely?
> > You can call depwarn() directly from inside these functions, and print
> > a custom message like "iteration over FooCollection is deprecated". See
> > base/deprecated.jl for examples.
> >
> >
> > Regards