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: 103376,d650bb993a154b84 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!h11g2000prf.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Learning tagged types Date: Fri, 22 Feb 2008 16:04:11 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <31ec7e11-3cd0-4f3c-ad11-c06e7edfb9cb@e10g2000prf.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1203725051 25673 127.0.0.1 (23 Feb 2008 00:04:11 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 23 Feb 2008 00:04:11 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: h11g2000prf.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:20004 Date: 2008-02-22T16:04:11-08:00 List-Id: On Feb 22, 3:23 pm, mhamel...@yahoo.com wrote: > Hello, this is a representative bit of code I use and, experimenting > with tagged types, want to know if/how this can be cleanly moved to a > more object oriented format. I think I want proc and def to be > abstract procedures, that requires the "object" Proc_Def itself to be > abstract, that means declaring new packages everytime I have a new > Proc_Def? Thanks for any pointers (no pun intended), > > with Text_Io; > with Ada.Containers.Doubly_Linked_Lists; > > procedure Tag_Test2 is > > type Proc_Def; > > type Proc_Ptr is access procedure (Def : Proc_Def; > Val : Float); > type Term_Ptr is access procedure (Def : Proc_Def); > > type Proc_Def is > record > Name : String (1 .. 4); > Proc : Proc_Ptr; > Term : Term_Ptr; > end record; I think what you want is to declare a root type instead of Proc_Def. I don't know what you really want to call it, but you probably want to make it an abstract type; then, instead of Proc_Ptr and Term_Ptr, you'd declare "Proc" and "Term" or something like that to be primitive operations of the root type. E.g. type Root is abstract tagged record Name : String (1 .. 4); end record; procedure Proc (Def : Root; Val : Float) is abstract; procedure Term (Def : Root) is abstract; You do need to declare Root in a package specification, and Proc and Term in the same specification. (But the package could be nested inside a procedure, if you really want it to.) Then whenever you want to declare a particular version of this with its own versions of Proc and Term (such as your Proc1 and Term1 below), you'd declare a type extension: type Some_Type is new Root with null record; overriding procedure Proc (Def : Some_Type; Val : Float); overriding procedure Term (Def : Some_Type); ("overriding" is an Ada 2005 extension; it's not required, but is there to help protect against accidents. In Ada 95, just leave that word off.) A caveat: In Ada 95, Some_Type can't be declared inside a procedure unless Root was declared earlier inside that same procedure. In Ada 2005, this restriction is relaxed, but I still think you'll have to follow it if you use it with Doubly_Linked_Lists---otherwise you could create a list that points to a Some_Type object after Some_Type is no longer visible. Of course, Some_Type doesn't have to say "with null record". If you like, you can add additional fields---in fact, one of the purposes of a more object-oriented style would be so that you can have additional data for Some_Type that is just for that type and not for every type in the class. Now, instead of Proc_Def in the following declarations, you'd use Root'Class since the type could be any type in the class: > package DLL is new Ada.Containers.Doubly_Linked_Lists (Proc_Def); > Proc_List : DLL.List; > > procedure Insert_Proc (Def : Proc_Def) is > begin > Proc_List.Append (Def); > end Insert_Proc; > > function Retrieve_Proc return Proc_Def is > begin > return Proc_List.First_Element; > end Retrieve_Proc; The following would be the overriding bodies of Proc and Term for your Some_Type type: > procedure Proc1 (Def : Proc_Def; -- change this to Some_Type > Val : Float) is > begin > Text_Io.Put_Line (Def.Name & ": processing - " & Val'Img); > end Proc1; > > procedure Term1 (Def : Proc_Def) is -- change this to Some_Type > begin > Text_Io.Put_Line (Def.Name & ": shutting down"); > end Term1; > > Test_Proc : Proc_Def; > > begin > Insert_Proc (("test", Proc1'access, Term1'access)); The above would become Insert_Proc (Some_Type' (Name => "test")). > Test_Proc := Retrieve_Proc; > Test_Proc.Proc (Test_Proc, 5.0); > Test_Proc.Term (Test_Proc); > end Tag_Test2; To answer your other question: no, you don't have to declare a new package for every new type in the class. Some programmers like doing that, but that's a matter of style. I haven't actually tried this. You may have to fill in some of the details, and I could have gotten one or two things wrong. But hopefully this will at least get you started. -- Adam