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,cce4354abe7758ef X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Changing default values in derived types Date: Sun, 30 Aug 2009 12:07:14 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <18c7529c-1dcb-416d-a1ae-71a713ebed45@n2g2000vba.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1251648435 23827 192.74.137.71 (30 Aug 2009 16:07:15 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 30 Aug 2009 16:07:15 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:606CbbtjSec0jPA8CPaVA9xgsig= Xref: g2news2.google.com comp.lang.ada:8059 Date: 2009-08-30T12:07:14-04:00 List-Id: vlc writes: > is there a way to change a default value upon type derivation? > > type PT is tagged record > V : Float := 1.0; > end record; > > type CT is new PT with null record; > > Is there a way to use another default value for V in CT? No, not directly. But you could do something like this: package PTs is type PT (<>) is tagged private; function Make return PT; private type PT is tagged record V : Float; end record; end PTs; package body PTs is function Make return PT is begin return (V => 1.0); end Make; end PTs; package PTs.CTs is type CT (<>) is new PT with private; function Make return CT; private type CT is new PT with null record; end PTs.CTs; package body PTs.CTs is function Make return CT is begin return (V => 1_000.000_001); end Make; end PTs.CTs; with PTs.CTs; use PTs; use PTs.CTs; procedure Main is X : PT := Make; -- Initialize V to 1.0. Y : CT := Make; -- Initialize V to 1_000.000_001. begin null; end Main; The (<>) on the type declarations means "clients are not allowed to create uninitialized objects". So "X : PT;" is illegal in Main. Main is forced to explicitly initialize, and the only way to get a value is to call Make. You might want to call them Make_PT and Make_CT, but then you wouldn't want them to be primitive -- either have them return the class-wide type, or declare them in nested packages. I think default values are a questionable feature anyway, so I'd generally prefer something like the above. And in Ada 2005, it even works for limited types. >...I already > tried with controlled types using different Initialize procedures, but > that's quite ugly. And fairly expensive, on most implementations, although AdaCore is working on making controlled types more efficient. > Thanks a lot in advance! You're welcome. - Bob