Thanks Ian, the state machine case reminds me of a video by Rob Pike: https://www.youtube.com/watch?v=HxaD_trXwRE , put the link here to help others who are interested :-)
On Tuesday, March 16, 2021 at 12:58:00 PM UTC+8 Ian Lance Taylor wrote: > On Mon, Mar 15, 2021 at 9:23 PM messi...@gmail.com > <messi...@gmail.com> wrote: > > > > In file cycles.src there're many type declarations to demo cycle > reference, some are invalid and some are valid, I can understand why > invalid ones are invalid, but for some of the valid ones, I cannot figure > out their usage scenarios, take some as examples: > > > > type T *T // Does nil the only value a type T variable/const can hold? > > > > or with bigger cycle: > > type ( // The same with above T for all T1, T2 and T3? > > T1 T2 > > T2 *T3 > > T3 T1 > > ) > > I'm not aware of any practical use for this. However, in Go we try to > aim for simplicity and orthogonality where possible, even if that > permits writing code that is not particularly useful. Rather than > write a special rule like "pointer types may not be a loop," we simply > permit it to work and avoid having another rule in the language. > > There is an amusing use of this kind of type in > https://golang.org/test/peano.go, but it's not intended to be > practical or useful. > > > > or function type decl like: > > > > F func(F) F > > > > I'm wondering whether type decls like this do have practical usage, or > just for grammar learning? > > Types like > > type F func() F > > on the other hand, are useful for state machines. The idea is that > the current state of some operation is expressed in the form of a > function, and every call to the state function returns a function that > implements the next state. There is an example of this at > https://golang.org/src/text/template/parse/lex.go#L105. > > Ian > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1ed7651f-8288-4f08-b5ad-d909f7bb5f53n%40googlegroups.com.