On Mon, Mar 15, 2021 at 9:23 PM messi...@gmail.com
<messi.sh...@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/CAOyqgcXqKAYSLxgj%2BuZfsdgqet%3Dk7F%2BW33a4WYSTb7aXpB7UZg%40mail.gmail.com.

Reply via email to