comp.lang.ada
 help / color / mirror / Atom feed
* Array Literals?
@ 1996-08-12  0:00 Spasmo
  1996-08-13  0:00 ` Jon S Anthony
  1996-08-13  0:00 ` John Herro
  0 siblings, 2 replies; 6+ messages in thread
From: Spasmo @ 1996-08-12  0:00 UTC (permalink / raw)



Hey all.  I've got a question about array literals.
I looked through the RM (checked out the index and
hopped to the keywords that looked promising) but
I couldn't find the answer to this question.

Ok for my question.  Is it valid on all Ada
implementations to provide array literals to 
functions and the like?  I know that Gnat let
me do it but I'd just like to make sure this
is guaranteed to work regardless of implementation.

For example:

type Int_Array is array(Integer range <>) of Integer;

procedure Print_Int_Array ( I_List : Int_Array ) is
begin
	for I in I_List'Range loop
		Put_Line( Integer'Image ( I_List(I) ) );
	end loop;
end Print_Int_Array;


Now when we call it, is it standardly acceptable to
call it as such:

	Print_Int_Array ( (1, 2, 3, 4) );

Where (1, 2, 3, 4) is an array literal?  I know it
works in GNAT3.05 but I want to make sure this isn't
a fluke and that I can depend on this behavior
regardless of which implementation I use.

So this would mean that array literals are in fact
flexible enough to be used anywhere an array can
be used eh?  

Thanks in advance.

--
Spasmo
"Everyone has secrets, but sometimes you get caught,
 So if it's just between us, my silence can be bought"
	"Blackmail" by Sloppy Seconds





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Array Literals?
  1996-08-12  0:00 Array Literals? Spasmo
  1996-08-13  0:00 ` Jon S Anthony
@ 1996-08-13  0:00 ` John Herro
  1996-08-13  0:00   ` Spasmo
  1996-08-14  0:00   ` Robert I. Eachus
  1 sibling, 2 replies; 6+ messages in thread
From: John Herro @ 1996-08-13  0:00 UTC (permalink / raw)



cosc19z5@Bayou.UH.EDU (Spasmo) asks if the
following will work with any standard Ada compiler:
> type Int_Array is array(Integer range <>) of Integer;
> procedure Print_Int_Array ( I_List : Int_Array );
> Print_Int_Array ( (1, 2, 3, 4) );
     Surely!  That's standard Ada.  But there's one caution.  In your
simple example, there's only one procedure called Print_Int_Array, and
thus, when the compiler sees the call, it knows that the aggregate
(1,2,3,4) must be of type Int_Array, and there's no problem.  If you had
had several overloaded procedures with that name, each taking a different
type, and if the aggregate (1,2,3,4) would fit more than one of those
types, then your call would be ambiguous and wouldn't compile.  In that
case, you would have to *qualify* the aggregate, thus:
Print_Int_Array(Int_Array'(1,2,3,4));
     Note the ' with the ( ).  Sometimes, for clarity, it's a good idea to
qualify the aggregate anyway, so the reader will know what type (1,2,3,4)
is.
     Without realizing it, you've been using "array literals for functions
and the like" whenever you've written a quoted string in a subprogram
call!  For example, Put_Line("Hello"); is an abbreviation for
Put_Line(('H','e','l','l','o'));.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
A: "PS/2 Microchannel, GPIB, and Greyhound."
Q: "Name two dogs and a bus."




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Array Literals?
  1996-08-12  0:00 Array Literals? Spasmo
@ 1996-08-13  0:00 ` Jon S Anthony
  1996-08-13  0:00 ` John Herro
  1 sibling, 0 replies; 6+ messages in thread
From: Jon S Anthony @ 1996-08-13  0:00 UTC (permalink / raw)



In article <4uo9b6$qj1@Masala.CC.UH.EDU> cosc19z5@Bayou.UH.EDU (Spasmo) writes:

> type Int_Array is array(Integer range <>) of Integer;
> 
> procedure Print_Int_Array ( I_List : Int_Array ) is
> begin
> 	for I in I_List'Range loop
> 		Put_Line( Integer'Image ( I_List(I) ) );
> 	end loop;
> end Print_Int_Array;
> 
> 
> Now when we call it, is it standardly acceptable to
> call it as such:
> 
> 	Print_Int_Array ( (1, 2, 3, 4) );
> 
> Where (1, 2, 3, 4) is an array literal? 

Yes.  That is simply an array aggregate and the type comes from the
context which sez it is an Int_Array.  No different than

    a : Int_Array := (1, 2, 3, 4);


> I know it
> works in GNAT3.05 but I want to make sure this isn't
> a fluke and that I can depend on this behavior
> regardless of which implementation I use.
  ^^^^^^^^^^

Well, the implementation could always be broken! :-)
But, you should be OK.


> So this would mean that array literals are in fact
> flexible enough to be used anywhere an array can
> be used eh?

If the shoe fits!


/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Array Literals?
  1996-08-13  0:00 ` John Herro
@ 1996-08-13  0:00   ` Spasmo
  1996-08-14  0:00     ` John Herro
  1996-08-14  0:00   ` Robert I. Eachus
  1 sibling, 1 reply; 6+ messages in thread
From: Spasmo @ 1996-08-13  0:00 UTC (permalink / raw)



John Herro (johnherro@aol.com) wrote:
: cosc19z5@Bayou.UH.EDU (Spasmo) asks if the
: following will work with any standard Ada compiler:
: > type Int_Array is array(Integer range <>) of Integer;
: > procedure Print_Int_Array ( I_List : Int_Array );
: > Print_Int_Array ( (1, 2, 3, 4) );
:      Surely!  That's standard Ada.  But there's one caution.  In your
: simple example, there's only one procedure called Print_Int_Array, and
: thus, when the compiler sees the call, it knows that the aggregate
: (1,2,3,4) must be of type Int_Array, and there's no problem.  If you had
: had several overloaded procedures with that name, each taking a different
: type, and if the aggregate (1,2,3,4) would fit more than one of those
: types, then your call would be ambiguous and wouldn't compile.  In that
: case, you would have to *qualify* the aggregate, thus:
: Print_Int_Array(Int_Array'(1,2,3,4));

Makes plenty of sense.  


:      Note the ' with the ( ).  Sometimes, for clarity, it's a good idea to
: qualify the aggregate anyway, so the reader will know what type (1,2,3,4)
: is.

Yep.  Question though, what exactly is the difference between using
Int_Array' ( ), and Int_Array ( )?  I have used the latter before
to perform conversion with no apparent ill effect, but I believe
there is a difference, I just can't seem to recall it.


:      Without realizing it, you've been using "array literals for functions
: and the like" whenever you've written a quoted string in a subprogram
: call!  For example, Put_Line("Hello"); is an abbreviation for
: Put_Line(('H','e','l','l','o'));.

Well yes I knew that string literals were a form of array literal,
but some languages have special considerations for strings so
that you could treat strings in certain ways that you may not
be able to treat standard arrays.  I just wasn't sure if Ada was
in fact this consistent in its treatment (I'm glad to see it
is).

Thanks.


: - John Herro
: Software Innovations Technology
: http://members.aol.com/AdaTutor
: ftp://members.aol.com/AdaTutor
: A: "PS/2 Microchannel, GPIB, and Greyhound."
: Q: "Name two dogs and a bus."

--
Spasmo
"Everyone has secrets, but sometimes you get caught,
 So if it's just between us, my silence can be bought"
	"Blackmail" by Sloppy Seconds





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Array Literals?
  1996-08-13  0:00 ` John Herro
  1996-08-13  0:00   ` Spasmo
@ 1996-08-14  0:00   ` Robert I. Eachus
  1 sibling, 0 replies; 6+ messages in thread
From: Robert I. Eachus @ 1996-08-14  0:00 UTC (permalink / raw)



In article <4uquru$jrq@Masala.CC.UH.EDU> cosc19z5@Bayou.UH.EDU (Spasmo) writes:

  > Yep.  Question though, what exactly is the difference between using
  > Int_Array' ( ), and Int_Array ( )?  I have used the latter before
  > to perform conversion with no apparent ill effect, but I believe
  > there is a difference, I just can't seem to recall it.

   Int_Array'(1,2,3,4,5) is a value of type Int_Array.
   Int_Array (1,2,3,4,5) won't work--assuming Int_Array is a type or
                         subtype name. 
   Int_Array (foo) converts foo to an Int_Array.
   Int_Array (Foo_Array'(1,2,3,4,5)) will convert the Foo_Array value
                 to an Int_Array.


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Array Literals?
  1996-08-13  0:00   ` Spasmo
@ 1996-08-14  0:00     ` John Herro
  0 siblings, 0 replies; 6+ messages in thread
From: John Herro @ 1996-08-14  0:00 UTC (permalink / raw)



cosc19z5@Bayou.UH.EDU (Spasmo) writes:
> What exactly is the difference between using
> Int_Array' ( ), and Int_Array ( )?
     The ' with ( ) indicates *qualification*.  That is, you're telling
the compiler what the type is, because, perhaps, it's not obvious from the
literal or the aggregate.  The ( ) without the ' indicates *conversion*
from one type to another.
     For example, suppose your Ada compiler implements type Long_Integer,
and you have
     J : Integer;
     procedure Display(Item : in Integer);
     procedure Display(Item : in Long_Integer);
Then writing Display(3); is ambiguous, so you would *qualify* the literal
and write Display(Long_Integer'(3));.  On the other hand, if you want to
*convert* J to Long_Integer, you use ( ) without the ' and write, for
example, Display(Long_Integer(J));
     Sometimes you need to qualify an enumeration literal.  This example
is from my Ada Tutor program (available for download at the Web and FTP
sites below my signature):
     type Rainbow_Color is (Red, Orange, Yellow, Green, Blue, Indigo,
Violet);
     type Traffic_Light Color is (Red, Amber, Green);
     procedure Display(Item : in Rainbow_Color);
     procedure Display(Item : in Traffic_Light_Color);
Now the compiler can handle Display(Violet); and Display(Amber);, but
requires the qualification in Display(Rainbow_Color'(Red));.  I hope this
helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
     A doctor, a civil engineer, and a software engineer were arguing
about who has the oldest profession.  The doctor said, "God made Eve from
the rib of Adam, a surgical operation."  The civil engineer said, "Before
He did that, God made order from chaos and put the planets in motion, a
feat of civil engineering."  The software engineer replied, "Wait a
minute!  Where do you think the chaos came from?"




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1996-08-14  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-12  0:00 Array Literals? Spasmo
1996-08-13  0:00 ` Jon S Anthony
1996-08-13  0:00 ` John Herro
1996-08-13  0:00   ` Spasmo
1996-08-14  0:00     ` John Herro
1996-08-14  0:00   ` Robert I. Eachus

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