On Mon, May 3, 2021 at 6:44 PM Delta Echo <deltaecho...@gmail.com> wrote:

> Hi, Is there any document that explains how recursive type definitions like
>
> type stateFn func(*Scanner) stateFn
>
> are handled in Go?

Not sure what "handle" means in this case. The language specs do not
mention recursive types, so what follows is my opinion only.

A recursive type is a type size of which cannot be computed because
for that you need to know its size in the first place.

So `type t struct { t }` is recursive: https://play.golang.org/p/M64qW_XnnsG

But `type t struct { *t }` is not:
https://play.golang.org/p/7EE2hzcRst1 because the fixed size of a
pointer "breaks" the recursive chain.

In this ad hoc definition `type stateFn func(*Scanner) stateFn` is not
recursive and thus compiles just fine:
https://play.golang.org/p/sLDd4K58FDG

-- 
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/CAA40n-UJ5Yj6akZbKXq4AY0n6XJE9JU1Ot_LXgLSVFxxmRPGDA%40mail.gmail.com.

Reply via email to