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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ebe6e2ef707c0384 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 02 Nov 2006 22:59:05 -0600 Date: Thu, 02 Nov 2006 23:48:43 -0500 From: Jeffrey Creem User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: how to create a record References: <1162523698.802339.225760@h54g2000cwb.googlegroups.com> In-Reply-To: <1162523698.802339.225760@h54g2000cwb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 24.147.74.171 X-Trace: sv3-hYQ1smRDtHpIg7x0Y5jbwmR3dKZueC3EEtls8AmVHLTCo6rg9dlxMHGY4du904Iev4g9qRMFY0YroSI!zg/e28TcCdxe6xNeuXx1Qmryy6a00xZwUYFvdLVAOZo33cFFVqup4hsCmv2LPafaMO6mFVzuQEnc!SQI= X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:7339 Date: 2006-11-02T23:48:43-05:00 List-Id: 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.