-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: spec: range over nil function values should not panic #65629
Comments
Seems more logically consistent and it makes it easier to return a blank iterator in different spots. You might do |
Personally I am somewhat torn on this one. Ranging over a I think both views are somewhat reasonable. |
ISTM there would be expected behaviors for the most referentially transparent If |
As a side effect if we change this that means any manual handling of the iterator will need to manually handle both. func Concat[V any](iterators ...iter.Seq[V]) iter.Seq[V] {
return func(yield func(V) bool) bool {
for _, i := range iterators {
if !i(yield) {
return false
}
}
return true
}
} vs: func Concat[V any](iterators ...iter.Seq[V]) iter.Seq[V] {
return func(yield func(V) bool) bool {
for _, i := range iterators {
+ if i == nil {
+ continue
+ }
+
if !i(yield) {
return false
}
}
return true
}
} |
Iterators are functions; calling nil functions panics. If a direct call and a use in range behaved differently, then that would be a gotcha when thinking about having to convert code from one form to the other. Nil iterators are a mistake and should not be used, same as nil pointers. |
Some ranges do panic by the way: https://go.dev/play/p/B_tw9VJ11ZU |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Proposal Details
Environment: Go 1.22 with GOEXPERIMENT=rangefunc
Currently, we can iterate nil slice and nil map with
for range
and nil channel blocks forever.But, nil
iter.Seq[T]
panics.I think it's better if we can iterate nil sequence like nil slice.
Also, I can't find this is desinged or not at https://go.dev/wiki/RangefuncExperiment.
The text was updated successfully, but these errors were encountered: