comp.lang.ada
 help / color / mirror / Atom feed
From: munnari.oz.au!ariel!ucsvc.ucs.unimelb.edu.au!phillip.edu.au!dale@tcgould.
Subject: Re: Uninitialized subtype variables
Date: 27 Oct 92 01:22:56 GMT	[thread overview]
Message-ID: <1992Oct26.202256.13240@phillip.edu.au> (raw)

On the subject of initialisation one problem that I encounter often in 
teaching CS1 Ada course is that of an out parameter that is not
initialized.

For example the following search procedure 


	procedure search(x    :in     string10;
                         key  :in     character;
                         found:   out boolean;
                         index:   out string10_range);


will set index to a value _only_ if the item is found (why should it do 
anything else?).

If the actual parameter associated with index is subtyped, then not finding
'key' often results in a constraint error. This is particuarly galling when
the actual for index is itself not initialised (why should it be?), and 
(because of the copy in/copy out semantics of scalars) produces the erroneous
result in the first place. 

The _only_ solution for first year students is to say "yes well that is a 
problem. Why don't you just make it an in out parameter and forget about it"
(well, this is almost what I say... :-) );

The other solution is to create a variable with a set/unset field...

         type result(found :boolean ) is
                record
                        case found is
                                when false => null;
                                when true  => index  :string10_range;
                        end case;
                end record;

and then modifying the procedure search accordingly.

Certainly simulating return values like this is the pure (but painful) way of
maintaining the correct parameter modes for a procedure such as search.

???
Perhaps variables should have a special value (Null) that indicates that 
they have not been initialised?

???
Perhaps another field could be set aside to maintain the initialisation
status *by the compiler*

???
Perhaps a pragma could be provided to disable this useful (development?)
feature.

Remember the days when array index checking was not the norm?
First it came in during program testing, and now during program operation?
Will variable initialisation testing follow?

---------------------------------------------------------------------------
Dale Stanbrough
Royal Melbourne Institute of Technology
Melbourne, Australia
dale@phillip.edu.au

             reply	other threads:[~1992-10-27  1:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-10-27  1:22 munnari.oz.au!ariel!ucsvc.ucs.unimelb.edu.au!phillip.edu.au!dale [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-10-27  1:26 Uninitialized subtype variables Robert I. Eachus
1992-10-25 11:37 math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sunic!seunet!enea!s
1992-10-24 18:44 Alex Blakemore
1992-10-23 20:43 Michael Feldman
1992-10-23 16:49 cis.ohio-state.edu!news.sei.cmu.edu!ajpo.sei.cmu.edu!falis
1992-10-22 17:38 bloom-picayune.mit.edu!enterpoop.mit.edu!snorkelwacker.mit.edu!tamsun.tam
1992-10-22 13:46 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wupost!micro-heart-of-gold.m
1992-10-21 23:47 Richard Pattis
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox