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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,c7ff90b15f15636c X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!newsfeed101.telia.com!nf02.dk.telia.net!news.tele.dk!news.tele.dk!small.news.tele.dk!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Sun, 11 Oct 2009 20:02:48 +0300 From: Niklas Holsti Organization: Tidorum Ltd User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Runtime type selection References: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com> In-Reply-To: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4ad20fd5$0$26308$4f793bc4@news.tdc.fi> NNTP-Posting-Host: 81.17.205.61 X-Trace: 1255280597 news.tdc.fi 26308 81.17.205.61:37128 X-Complaints-To: abuse@tdcnet.fi Xref: g2news2.google.com comp.lang.ada:8668 Date: 2009-10-11T20:02:48+03:00 List-Id: xorque wrote: > Hello. > > I'd like to develop a program that processes a graph. The contents of > the graph are a random selection of objects of different types (all > elements may be of the same type or may all be different - assuming > there are enough types defined). By 'type' in this case, I am > referring > to Ada types. > > The basic problem is that I want to create an object of a type picked > at > random, at runtime. I'm not entirely sure what my options are here. > > I could create a base tagged type and derive a set of types from that > base, selecting at random. I could use variant records. It seems that > in > either case, I'd need to create an enumeration type with a 1:1 mapping > between enumeration values and derived types. > > Both of these solutions seem a little unwieldly. They also seem like > they'd end up making the program difficult to extend (I'd ideally just > like to derive a new type and have it automatically available to be > selected at random for the graph). Keep a list or array of (accesses to) a factory function for each possible (derived) type. When you derive a new type from the base graph-element type, add its factory function to this list (this is often called "registering" the new type/function). When you want to make a graph element of a "random" type, pick a random index into the list or array of factory functions and call the chosen function, which creates the new element and returns an access to it (type access all Base_Graph_Element_Type'Class). -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .