[go-nuts] Re: Custom types for use with range (Go 2)

2018-08-07 Thread Christophe Meessen
If considering user defined Ranger interface, I would strongly suggest to 
look at D's equivalent. The interface is simple and there are many use of 
it. It is trivial to develop a reverse range or ranges that traverse data 
structures (e.g. binary trees) in different ways (once you have generics).

My experience with it (as a beginner) was that it makes the code less 
readable. There is a tendency to overuse it because a "for range" is 
concise and looks simple. But it is not because one has to lookup the 
"Ranger" implementation to understand what it is doing. When you know by 
hart what each "Ranger" does, you can still read and understand the code, 
but this is for the elites. It may give an impression of power to those 
mastering it, but it gives an impression of frustration to people learning 
the language. This also adds the burden to decide which "Ranger" to use, or 
to determine if there is a Ranger for what I want to do. 

The power of Go is its simplicity and readability. Let it stay that way. As 
it has already been said, it's only syntactic sugar.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Custom types for use with range (Go 2)

2018-08-07 Thread Matt Sherman
Sorry for late reply: yes, it’s sugar, and a first implementation might be 
to have the compiler simply rewrite it like a macro, as in your example.

And I realize that my example was more verbose than need be. We don’t call 
an iterator on arrays, maps, etc, so my example should have been:

for t := range tokenizer { 
   // etc 
}

I.e., need to call .Range(), since the point of the ‘interface' is to let 
the compiler infer how to iterate.

It’s quite a lot less boilerplate, while keeping the intent clear, and 
maybe even preventing some classes of user error.


On Friday, July 20, 2018 at 9:24:18 AM UTC-4, Juliusz Chroboczek wrote:
>
> > for t := range tokenizer.Next() { 
> > // etc 
> > } 
>
> Isn't that just syntactic sugar for 
>
> for t, more := f(); more; t, more = f() { 
> ... 
> } 
>
> ? 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.