iterator method naming conventions

158 views
Skip to first unread message

Adam Baratz

unread,
Aug 22, 2024, 12:30:52 PM8/22/24
to golang-nuts
Hi,

I've been reading the iter pkg docs and I'm trying to understand a distinction drawn there.

It says that the iterator method on a collection type is conventionally named All (and returns a Seq). However, the maps and slices packages handle this a little differently. They have All funcs that return a Seq2, and separate Values funcs that return a Seq.

How come there's this difference? Looking at making an update to some collection types, wondering if it's more appropriate to have a Values method if it's going to return a Seq.

Thanks,
Adam

Ian Lance Taylor

unread,
Aug 22, 2024, 12:52:33 PM8/22/24
to Adam Baratz, golang-nuts
For a container type that contains only values, not keys, All should
return an iter.Seq.

For a container type that contains both keys and values, All should
return an iter.Seq2.

Slices and maps are container types that contain both keys and values,
so maps.All and slices.All return iter.Seq2.

Whether a Values method/function makes sense is going to depend on the
container type. For both slices and maps it does make sense (and, of
course, maps also has a Keys function).

Ian
Reply all
Reply to author
Forward
0 new messages