Yield after break

Yield after break #

It is important that iter.Seq implementations honour the false return from yield and exit the loop. If this is not honoured then go optics will panic in order to break out of the loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
	result, err := Get(
		SliceOf(
			Taking(
				TraverseCol[int, int](),
				1,
			),
			0,
		),
		Col(
			func(yield func(focus int) bool) {
				i := 0
				for {
					//false return from yield is ignored here.
					yield(i)
					i++
				}
			},
			nil, //default Length getter
		),
	)

	fmt.Println(result, err)

In this case after the Taking(..,1) will cause the yield to return false after he first element causing a yieldAfterBreak panic. Not handling the false return value from a yield function is an implementation error and should be corrected in the source code.