-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: (Period[T]) Periodic
and similar methods returns iter.Seq[Time[T]]
instead of channel
#34
Comments
Hello @nobishino !! Thank you for describing the proposal. I have indeed considered iterators in the past. Regarding the proposal you have presented, honestly, I don't feel like the benefits outweigh the disruption to backward compatibility. func (p Period[T]) Periodic(next func(Time[T]) Time[T]) periodical[T] If there are benefits to implementing the iterator interface, perhaps we could provide an What do you think? |
Thank you! I would add some more detail and options for my proposal. First of all, my proposal is motivated not by specific use-case, but by my personal technical curiosity.
Thanks for feedback! Based on that, I came up with several options which keeps backward compatibility as follows. Option 1: Add no new API for
|
Thank you for the wonderful proposal of Pros and Cons, @nobishino. It has become clear to me how we can approach each of them. The policy I support is as follows:
As for the direction, I think Option 2, 2-2 is better. Although it was mentioned that it might become complicated, I believe that by implementing it in this way, where all processing for iteration is enclosed in the func (p periodical[T]) Seq() iter.Seq[Time[T]] {
return func(yield func(Time[T]) bool) {
for current := range p {
if !yield(current) {
break
}
}
go func() {
for current := range p {
// Prevents leakage of the sending goroutine by reading
// all channels sent to periodical[T].
}
}()
}
} |
@Code-Hex Oh, that is very nice! I didn't come up with that receiver-side solution. Now I am convinced that Option 2-2 (with your implementation) is good idea. |
I would submit a PR when Here is WIP branch: nobishino@ce1f98f |
@nobishino I'm sorry for late response. |
golang/go#61897 がacceptedになりました。
これに伴い、コレクションを返すAPIでは
iter.Seq
を返すことがおそらく一般的になっていくと思います。例えばGo標準ライブラリのproposal golang/go#53987 (comment) は、最終的に
iter.Seq
を返すAPIとしてacceptされました。同様に、synchroでコレクションを返すAPIを
iter.Seq
に対応させるプランはあるでしょうか?(もしあれば、実装することに興味があります)具体的には以下のようなものです。
細かいところが十分考えられていないのですが、少し見ていただいた印象としてもしポジティブであればより具体的に考えたいと思っています。
Proposal
synchro
ではコレクションを返すメソッドとしてPeriod[T]
などがunexportedな型periodical[T]
を返しており、これの実体は<-chan Time[T]
になっていると思います。これの代わりに、iter.Seq
を返すようにするというProposalです。Edit: 最初aliasを使う形で書いていたのですが、言語仕様上できないことに気づき、修正しました。
メリット
iter.Seq
を前提としたAPIと組み合わせられるようになること実は具体的なユースケースが手元にあるわけではなく、これを提供することで大きいメリットがあるかどうかはわかっていません。
互換性などについてのノート
nobishino@1fd8814 でテストを書きましたが、少なくとも一部で後方互換性がありません。
periodical
型がchannelなので、channel型の変数に代入できるという性質(テストのProperty 1)があった。periodical
の型定義をiter.Seq
に置き換えると、この代入はできなくなる。periodical
型がchannelなので、for ~ range文で直接使うこともできた(テストのProperty 2)。この性質はrange over funcが実装された後のGoバージョンでは維持される。Slice
メソッドを削除する必要が生じる。Slice
メソッドを定義できるようにtype periodical[T TimeZone] iter.Seq[Time[T]]
を戻り値とした場合、戻り値の値をiter.Seq[Time[T]]
型の変数(関数の引数も含む)にそのままでは代入できなくなる。(型変換をすればできると思われる)The text was updated successfully, but these errors were encountered: