Skip to content

spec: for-range iterator yield function requires bool result - consider generalizing to any boolean type #71164

Open
@griesemer

Description

@griesemer

Per the spec, a "for-range" statement using an iterator requires the iterator's yield function to return bool not an arbitrary (user-defined) boolean:

Range expression                                       1st value                2nd value

array or slice      a  [n]E, *[n]E, or []E             index    i  int          a[i]       E
string              s  string type                     index    i  int          see below  rune
map                 m  map[K]V                         key      k  K            m[k]       V
channel             c  chan E, <-chan E                element  e  E
integer value       n  integer type, or untyped int    value    i  see below
function, 0 values  f  func(func() bool)
function, 1 value   f  func(func(V) bool)              value    v  V
function, 2 values  f  func(func(K, V) bool)           key      k  K            v          V

We should generalize this to any boolean type, similarly to how we allow any string type (not just string) when we range of strings.

Note that the original implementation of the type-checker accepted any boolean type, but the compiler's front-end had a problem with it (#71131). The (temporary) fix for that issue was to adjust the type-checker to match the spec literally. This avoided a compiler panic.

We should change the spec to reflect the original intent, and then revert the fix for #71131.

Metadata

Metadata

Labels

LanguageChangeSuggested changes to the Go languageLanguageProposalIssues describing a requested change to the Go language specification.NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.early-in-cycleA change that should be done early in the 3 month dev cycle.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions