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, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,22ea9f82aa6070da X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: Variant record question Date: 2000/01/21 Message-ID: <86ahu8$ke8$1@nnrp1.deja.com>#1/1 X-Deja-AN: 575847853 References: <4V0i4.622$dw3.29725@news.wenet.net> X-Http-Proxy: 1.0 x21.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Fri Jan 21 21:09:40 2000 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.7 [en] (WinNT; I) Date: 2000-01-21T00:00:00+00:00 List-Id: In article <4V0i4.622$dw3.29725@news.wenet.net>, "Mike Silva" wrote: > I've been working on some proof-of-concept code, and I don't > understand something about arrays of variant records. Specifically, > the following code works with a default discriminant defined for the > type T_t, but if I remove the default discriminant it fails at the > declaration of T_array_t. Why must I put in a default discriminant > when I can then override it in an aggregate (variable 'A' below)? I have personally never written an Ada compiler, but below is roughly the dialog that I imagine is going on between a developer and the compiler when a discriminated type is declared. Hopefully the experienced compiler writers in the crowd will correct anything that I got a bit wrong. When you declare a discriminated type without defaults for its discriminants, you are effectively telling the compiler: "I'll tell you how big objects of this type are when I create them." When the compiler needs to know the objects' size (eg, so it can write the machine code to perform an assignment) It may need to keep an associated dope record with the size, or look at the discriminant and do a calculation. Suppose I want an array of these things. Now suppose I come along and ask for element 20. How does the compiler know where element 2000 is? It would have to look up the sizes of every element from 1 to 1999. That's not exactly the kind of effencicy we'd typically want out of an array! That kind of indexing is what records are for. When you give the type a default discriminant, you are effectively telling the compiler: "I don't know how big objects of this type are either; I may need to change it. So please just allocate enough space for the maximum that my discriminant values could ever require. And until I tell you otherwise, use this discriminant value." Now since every object of this discriminated type is the same size (the maximum it could ever need to be), array indexing can work just like it would with any other object. -- T.E.D. http://www.telepath.com/~dennison/Ted/TED.html Sent via Deja.com http://www.deja.com/ Before you buy.