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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f27fe80cb2c65260 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!k13g2000hse.googlegroups.com!not-for-mail From: george.priv@gmail.com Newsgroups: comp.lang.ada Subject: Re: How to build Objects containing itself? Date: Fri, 20 Jun 2008 15:42:08 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <4fa39546-2b0c-4579-bb26-c191c0c2f6d8@k37g2000hsf.googlegroups.com> NNTP-Posting-Host: 69.250.188.114 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1214001728 14690 127.0.0.1 (20 Jun 2008 22:42:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 20 Jun 2008 22:42:08 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k13g2000hse.googlegroups.com; posting-host=69.250.188.114; posting-account=VnNb3AoAAACTpRtCcTrcjmPX7cs92k1Q User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:792 Date: 2008-06-20T15:42:08-07:00 List-Id: On Jun 20, 6:06 pm, "snoopysal...@googlemail.com" wrote: > Hi! > > So, here's my next question about object orientation in Ada. > > In languages like Ruby, C++ or Java, the following construct is > possible (here in Java): > > class State { > HashMap transitions = new HashMap(); > > } > > Perhaps you're wondering, what this code is meant to be. Some time ago > I programmed a stack-structure simulating a trie as part of a > incremental search-algorithm. The idea was to have a state-object > containing a mapping of characters pointing to state-objects again. > So, the result is a data-structure containing instances of its own > data type again. > > As object orientation is a bit different in Ada, the previous code > example would be something like this in Ada: > > package State is > > type State is tagged > record > Transitions : Transition_Maps.Map; > end record; > > package Transition_Maps is new Ada.Containers.Ordered_Maps > (Key_Type => Character, > Element_Type => State, > "<" => "<", > "=" => "="); > use Transition_Maps; > > end State; > > As you can see, we have a "What had been there first? The hen or the > egg?"-problem: The State-record requires an instance of > Transition_Maps.Map which is declared after the State-record's > definition. But because Transition_Maps.Map needs a State as > Element_Type changing the order ends in the same problem. > > So, has anybody an idea, how to solve this paradox? > > Thanks, > Matthias I am not sure what do you mean by including C++ there, but in Java all objects are dynamic references. So in order to make your type equivalent to Java one should use 1) references when defining the map and 2) Advance declaration of types (that will be also true for C++). Otherwise you will have infinitely recursive type declaration that compiler guards against. type State; type State_Access is access all State; package Transition_Maps is new Ada.Containers.Ordered_Maps (Key_Type => Character, Element_Type => State_Access, "<" => "<", "=" => "="); use Transition_Maps; type State is tagged record Transitions : Transition_Maps.Map; end record;