From: Jeffrey Creem <jeff@thecreems.com>
Subject: Re: how to create a record
Date: Thu, 02 Nov 2006 23:48:43 -0500
Date: 2006-11-02T23:48:43-05:00 [thread overview]
Message-ID: <d8uq14-u8e.ln1@newserver.thecreems.com> (raw)
In-Reply-To: <1162523698.802339.225760@h54g2000cwb.googlegroups.com>
markww wrote:
> Hi,
>
> How can I define a record or a class to this source file and create an
> instance of it?
>
> with Ada.Text_IO;
>
> type struct_device is
> record
> major_number : Integer := 0;
> minor_number : Integer := 0;
> end record;
>
> procedure Ada_LinkedList is
> begin
> Ada.Text_IO.Put_Line("Hello, world!");
> Ada.Text_IO.Put_Line("just terrible!");
> end Ada_LinkedList;
>
>
> I tried creating a record like in the above example, and compiling with
> gnatmake but get the error:
>
> C:\>gnatmake C:\Ada_LinkedList.adb
> gcc -c -IC:\ -I- C:\ada_linkedlist.adb
> ada_linkedlist.adb:4:05: compilation unit expected
> gnatmake: "C:\ada_linkedlist.adb" compilation error
>
> Thanks
>
You can't declare anything in Ada in the scope where you declared it.
Records need to be declared inside of procedures or inside of packages.
So, to do something close to what you appear to be asking for (define
the record and create an instance of it) try this
with Ada.Text_IO;
procedure Ada_LinkedList is
type struct_device is
record
major_number : Integer := 0;
minor_number : Integer := 0;
end record;
My_Happy_Record : struct_device;
begin
Ada.Text_IO.Put_Line("Hello, world!");
Ada.Text_IO.Put_Line("just spiffy!");
end Ada_LinkedList;
A few thoughts though (most of them useless but think about them)
1) You are using two different conventions in your program above. Both
CamelCase and the Traditional_Ada_Underscore method. Most Ada code tends
to use _ since that is what the LRM packages do (e.g. text_io) but
whatever you pick, try to stick with one style
2) Can't really guess where this is going but "struct_device" sounds
pretty C like. There are no 'structs' in Ada. Again this is just a silly
style thing and may not matter but it is probably best just live in the
style of whatever language you use.
3) This is probably the most controvertial statement I'll make. Others
will disagree...but.. I am not a big fan of the initialize everything in
the record definition approach. It has its uses. It even makes a lot of
sense in a lot of cases, but it does not always make sense in every
case. There are two reasons I dont like it. a - As you start declaring
local variables of these things in procedures you take the hit of the
initialization in a lot of cases where you don't need it. yes the
optimizer can sometimes find those cases....but in practice, they don't.
b - While this safe practice of initialize everying feels good and
prevents the read from unitialiazed memory issues, it creates a new kind
of problem. You can still end up with code where you expect that some
path puts some meaningful data into some field but it does not. You feel
good because it is initialized but in fact, it is still initialized with
something that is not contextually meaningful. Now, if there is
something that makes sense, then my all means do it. If not, don't.
Initializing the fields prevents you from using various cool tools to
detect problems like this in your code statically (polyspace) or
dynamically (i.e. gnat with some of its cooler run time checks
(i.e. http://tinyurl.com/ygrut9)
4) I don't know what it is about Ada but people love the word so much
they feel the need to say it all the time. Ada_LinkList? Would you have
written CLinkedList? ...Note this one is a really silly comment and
probably mostly because it is late. It is always a bad sign when someone
writes back a response 5x longer than the question when they really have
only 2 lines worth of useful thoughts.
next prev parent reply other threads:[~2006-11-03 4:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-03 3:14 how to create a record markww
2006-11-03 4:48 ` Jeffrey Creem [this message]
2006-11-03 5:29 ` Jeffrey R. Carter
2006-11-03 7:51 ` Maciej Sobczak
2006-11-03 5:00 ` jimmaureenrogers
2006-11-03 5:19 ` Jeffrey R. Carter
2006-11-03 11:51 ` Martin Krischik
2006-11-03 16:56 ` Pascal Obry
2006-11-04 3:09 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox