From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e94a7e4f6f888766 X-Google-Attributes: gid103376,public From: Robert A Duff Subject: Re: Self-referential types Date: 1999/10/12 Message-ID: #1/1 X-Deja-AN: 535831020 Sender: bobduff@world.std.com (Robert A Duff) References: <7ttb4a$8mq$1@nnrp1.deja.com> Organization: The World Public Access UNIX, Brookline, MA Newsgroups: comp.lang.ada Date: 1999-10-12T00:00:00+00:00 List-Id: Ted Dennison writes: > I'm trying to create my first self referential type. But there's > obviously something about this technique I'm missing, because all > attempts produce compiler errors in multiple compilers. > > Below is a code sample that is taken directly from Cohen's AAASL, > section 11.8.3: > > procedure Test is > > type Cell_Type; > > type List_Type is access all Cell_Type; > > type Cell_Type is > limited record > Contents_Part : String (1..20); > Next_Cell_Part : List_Type := Cell_Type'Access; > Previous_Cell_Part : List_Type := Cell_Type'Access; > end record; > begin > null; > end Test; > > Compiling this with Gnat gives me: > Test.adb:10:43: non-local pointer cannot point to local object > Test.adb:11:43: non-local pointer cannot point to local object > > I don't see an accesability level problem here, ... X: List_Type; procedure Nested is Local: Cell_Type; begin X := Local.Next_Cell_Part; end Nested; Nested; ... X.all ... -- Here, X is a dangling pointer. That's the accessibility level problem. You need to use 'Unchecked_Access, and you need to be careful not to do the above. The language design tries to prevent dangling pointers unless you do something "unchecked". >...and it looks like it > thinks Cell_Type is an object, not a type. It *is* an object -- the "current instance" of the type (sort of like "self" or "this" in some other languages). - Bob -- Change robert to bob to get my real email address. Sorry.