* Re: how to create a record
2006-11-03 3:14 how to create a record markww
@ 2006-11-03 4:48 ` Jeffrey Creem
2006-11-03 5:29 ` Jeffrey R. Carter
2006-11-03 7:51 ` Maciej Sobczak
2006-11-03 5:00 ` jimmaureenrogers
` (3 subsequent siblings)
4 siblings, 2 replies; 9+ messages in thread
From: Jeffrey Creem @ 2006-11-03 4:48 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 4:48 ` Jeffrey Creem
@ 2006-11-03 5:29 ` Jeffrey R. Carter
2006-11-03 7:51 ` Maciej Sobczak
1 sibling, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2006-11-03 5:29 UTC (permalink / raw)
Jeffrey Creem wrote:
>
> 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
If you do use CamelCase, many of us will run your code through
reformatters that will change it to Camelcase. If you don't like seeing
that, use underscores to separate words in identifiers.
> 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.
There are also no switch statements, no breaks, no casts, ...
Using such terms implies (usually correctly) thinking at the low-level
of most C code. Ada code is often much higher level. Using the Ada terms
helps indicate that you're thinking at that higher lever (or helps
pretend that you are; all this stuff about the Language Shootout is
really concerned with low-level stuff that is an elephant most of the time).
Also, this is an Ada newsgroup; understanding C terms is not a
requirement. You can be sure your audience understands you by using Ada
terms here.
--
Jeff Carter
"I soiled my armor, I was so scared."
Monty Python & the Holy Grail
71
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 4:48 ` Jeffrey Creem
2006-11-03 5:29 ` Jeffrey R. Carter
@ 2006-11-03 7:51 ` Maciej Sobczak
1 sibling, 0 replies; 9+ messages in thread
From: Maciej Sobczak @ 2006-11-03 7:51 UTC (permalink / raw)
Jeffrey Creem wrote:
> 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?
Actually, there are C++ programmers that put 'C' in front of *every*
class name. And it's not to compensate for the lack of namespaces in
some stone-age compilers, it's just to say "this is a class".
I guess that it's Microsoft's MFC framework/library that promoted it
most widely with its broken variant of what was initially a "Hungarian
Notation". For your amusement:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_hierarchy_chart.asp
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 3:14 how to create a record markww
2006-11-03 4:48 ` Jeffrey Creem
@ 2006-11-03 5:00 ` jimmaureenrogers
2006-11-03 5:19 ` Jeffrey R. Carter
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: jimmaureenrogers @ 2006-11-03 5:00 UTC (permalink / raw)
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;
>
The error message is correct. You are attempting to create a type
outside any compilation unit. You need to either move the record
definition within the procedure, or create a package containing the
record.
If you move the record definition within the procedure it will look
like this:
with Ada.Text_IO;
procedure Ada_Linked_List is
type Struct_Device is record
Major_Number : Integer := 0;
Minor_Number : Integer := 0;
end record;
begin
Ada.Text_IO.Put_Line("Hello World!");
Ada.Text_IO.Put_Line("just terrible!");
end Ada_Linked_List;
Jim Rogers
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 3:14 how to create a record markww
2006-11-03 4:48 ` Jeffrey Creem
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
4 siblings, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2006-11-03 5:19 UTC (permalink / raw)
markww wrote:
>
> with Ada.Text_IO;
>
> type struct_device is
> record
> major_number : Integer := 0;
> minor_number : Integer := 0;
> end record;
This is a declaration. It goes in a declarative region, between "is" and
"begin".
> procedure Ada_LinkedList is
Like here.
> 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:
What you have is a record type declaration. To create a record object
you have to declare an object of the type:
Object : Struct_Device;
Since you declare no objects of the type, the declaration serves no
purpose, so you can achieve the same effect by deleting the type
declaration.
--
Jeff Carter
"I soiled my armor, I was so scared."
Monty Python & the Holy Grail
71
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 3:14 how to create a record markww
` (2 preceding siblings ...)
2006-11-03 5:19 ` Jeffrey R. Carter
@ 2006-11-03 11:51 ` Martin Krischik
2006-11-03 16:56 ` Pascal Obry
4 siblings, 0 replies; 9+ messages in thread
From: Martin Krischik @ 2006-11-03 11:51 UTC (permalink / raw)
markww schrieb:
> 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
Your question has been answered, so just some suggested reading:
http://en.wikibooks.org/wiki/Ada_Programming/Packages
Martin
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to create a record
2006-11-03 3:14 how to create a record markww
` (3 preceding siblings ...)
2006-11-03 11:51 ` Martin Krischik
@ 2006-11-03 16:56 ` Pascal Obry
2006-11-04 3:09 ` Randy Brukardt
4 siblings, 1 reply; 9+ messages in thread
From: Pascal Obry @ 2006-11-03 16:56 UTC (permalink / raw)
To: markww
markww a �crit :
> How can I define a record or a class to this source file and create an
> instance of it?
Frankly, the first thing to do would be to read an Ada text book. The
code you post is not Ada! You are going to loose lot of time learning
using comp.lang.ada!
Pascal.
--
--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
^ permalink raw reply [flat|nested] 9+ messages in thread