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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e1e2bc096a996632 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!news.cs.univ-paris8.fr!newsfeed.vmunix.org!newsfeed.hanau.net!newsfeed01.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: why can't we declare unconstrained objects ? Date: Sun, 12 Dec 2004 18:39:36 +0100 Organization: None Message-ID: <4663928.Bk8sRzjrO0@linux1.krischik.com> References: Reply-To: martin@krischik.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1102877317 03 11986 0c04XYG6te+RtT9 041212 18:48:37 X-Complaints-To: usenet-abuse@t-online.de X-ID: V923E-Zp8eqikJwGODSdlnpFXrG0nKNJLbDESEdyGGeMes5a3k+Mki User-Agent: KNode/0.8.0 Xref: g2news1.google.com comp.lang.ada:6907 Date: 2004-12-12T18:39:36+01:00 List-Id: Michael Mounteney wrote: > Is there a simple way in Ada of simulating C/C++ unions ? It seems to > me that this is gratuitously prevented, that is, it can be done with > safety, by extending an existing run-time check, but it is in fact > prevented by the compiler. > Hopefully the following commented source will illustrate my point. > > with Ada.text_IO; > > procedure unconstrained is > > -- Very simple discriminated type > type thing (what : Boolean) is You need to provide a default: type thing (what : Boolean := true) is to get a mutable type. > record > case what is > when false => > letter : character; > when true => > number : natural; > end case; > end record; Ada ada will only allocate the minimum needed storrage for the discriminated types. i.E. for false 8 bit and for true 32 bit and that cannot be changed later - unless the type is mutable - then the maximum is allocated. > -- No problems here: we provide a discriminant. > sample : thing := (false, 'X'); > > -- This is alright as well of course. > type thing_pointer is access all thing; > > -- This is also fine: a pointer to any `thing'. > indirect : thing_pointer; > > -- This causes a problem: I want an unconstrained `thing', one > -- that can be switched between holding a character and a number > -- but the initialisation makes it constrained. > sample2 : thing := (true, 12); > -- Omitting the initialisation doesn't work: > -- this just fails at compile-time. > sample3 : thing; As said, you need a mutable type to do that. > begin > -- Just reference a field in the `thing'. > Ada.text_IO.put (sample.letter); > > -- Create a new access object and access its field > -- This requires a RUN-TIME check that indirect.what is true. > indirect := new thing (true); > indirect.number := 12; > > -- This will generate a RUN-TIME failure of course. > indirect.letter := 'A'; > > -- Warning at compile-time, failure at run-time. I want to change > the > -- discriminant. Since the compiler will insert a run-time check > -- for field selection via an access, why not for a direct variable ? > sample2 := thing'(false, 'Z'); > end; With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com