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,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!o13g2000cwo.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: 9 Mar 2005 07:56:08 -0800 Organization: http://groups.google.com Message-ID: <1110383768.773379.61780@o13g2000cwo.googlegroups.com> References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110284070.410136.205090@o13g2000cwo.googlegroups.com> <395uqaF5rhu2mU1@individual.net> <1110377260.350158.58730@z14g2000cwz.googlegroups.com> NNTP-Posting-Host: 209.194.156.4 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1110383773 31219 127.0.0.1 (9 Mar 2005 15:56:13 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 9 Mar 2005 15:56:13 +0000 (UTC) In-Reply-To: <1110377260.350158.58730@z14g2000cwz.googlegroups.com> User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: o13g2000cwo.googlegroups.com; posting-host=209.194.156.4; posting-account=SqOfxAwAAAAkL81YAPGH1JdBwpUXw9ZG Xref: g2news1.google.com comp.lang.ada:8944 comp.lang.c++:44784 comp.realtime:1135 comp.software-eng:4693 Date: 2005-03-09T07:56:08-08:00 List-Id: Hans Malherbe wrote: > Somewhere in this thread Loduvic writes: > > * in Ada, loop variables (a) are constants and (b) do not exist outside > of the loop > > This is safer, but limiting. > In C++ may want to declare the variable outside the loop, > break out early and use the loop variable. Let me guess: You can't > break out early in Ada, right? I see your problem, and I think I see where you have been misled. Ada provides three looping forms. Only one, the "for" loop, uses an automatically created local loop variable. Ada does allow you to break out of a loop early. In fact, one of the looping forms normally depends on that ability. The looping forms in Ada are: Simple loop -> loop some actions; end loop; While loop -> while condition loop some actions; end loop; For loop -> for I in some_range loop some actions; end loop; The simple loop will always be an infinite loop unless you break out early. The Ada reserved word used to break out of a loop is "exit". In Ada "exit" does not exit the program, only the enlcosing loop. The Ada exit command is commonly used in response to some condition: if condition then exit; end if; This usage is so common that Ada provides a short-hand syntax to achieve the same ends: exit when condition; The exit command can be used in any of the loop forms. The simple loop is often used to provide a loop with the conditional test in the middle or at the bottom of the loop. The conditional test is the condition controlling the exit command. The while loop works just like you would expect in many other languages. The for loop is somewhat like a foreach loop in C and C++. It iterates through a discrete range of values. The loop control variable in the for loop is local to the loop and is read-only within the loop body. You should probably choose either the simple loop or the while loop if you want to use the value of a loop control variable outside the body of the loop. The only way to use that value outside the body of a "for" loop is to copy the value to a variable having a scope outside the loop. > > * assignment is not an operator; it is an operation which does not > return a value. Thus, bugs like "if (something = 0)" cannot exist. > > I like this, but it prevents chaining assignments, limiting > expressiveness slightly since chaining says "equalize" as opposed to > "assign, assign, assign". You have that correct. > > * case statements (Ada's equivalent of a switch in C++) are required > to handle all possible cases. Thus it is impossible to forget one. > And, of course, there is no "break;" crap in Ada. > > Prevents fall through. The Ada case statement is a little more useful than might appear from the description above. The general form of the Ada case statement is: case discrete_value is when value_list => ... some set of statements ... when others => ... some set of statements ... end case; The value_list can be in the form of: * a single value (i.e. 255) * a range of values (i.e. 1..5) * a discontinuous set of values (i.e. 1 | 3 | 5 | 17) The Ada case statement requires that there be a when clause for every possible value of the type being tested. This works because Ada also allows you to define your own discrete types and subtypes, including their valid value ranges. subtype Menu_Options is character range 'a'..'c'; option : Menu_Options; case option in when 'a' => do_action_a; when 'b' => do_action_b; when 'c' => do_action_c; end case; If, in the example above, I left out the "when 'c' =>" part the compiler will issue an error. The "when others =>" option is equivalent to the "default" option in C and C++. > > * the type system, when used appropriately, makes it possible for the > compiler to find semantic errors in addition to just syntax errors. > For example, you can declare that Numers_Of_Apples and > Numers_Of_Oranges cannot be mixed. This is not possible with C++'s > typedef. > > This is more in the C++ tradition. The programmer has choice. > In C++ you can extend the type system to achieve this and more > (someone mentioned dimensional analysis), just not with typedef. > True. C++ provides strong type separation through the use of classes. > * accessibility rules are rather complex, but they are designed to > minimise the chance of mistakes. Basically, the scope of a pointer > type must be included in the scope of the pointed-to type. This > makes many mistakes impossible, such as returning a pointer to an > object which no longer exists. > > This looks like a limitation, but I'm not sure I understand correctly. > Example please! Ada does not provide any generic pointer type equivalent to void*. You must define each pointer type you want to use. Objects of that pointer type must be placed within the scope of the definition of their type. Outside that scope the type does not exist. This rule does not prevent all attempts to deallocate a null pointer. Null pointers can be found in any scope and are not only found when a type goes out of scope. > > A few other questions: > > Do you have nested functions like Pascal have? Can you access local > variables? Can you pass pointers to these functions around? Yes, Ada does allow nested functions, procedures, tasks, and even packages. A function can access any local visible local variable. Variables declared in an enclosing scope are visible within the nested function. You can create pointers to functions. This is yet another type of pointer you can create. Pointers to nested functions are only valid within the enclosing scope of the nested function. > > Can you pass non-type parameters (like the number 3.14) to templates? Yes, Ada generics allow values to be passed, including floating point values. The generic formal parameter for the value specifies what type the value can be. > > Can templates recurse? > Once a generic is instantiated it behaves like any other compilation unit. Recursion is allowed within generic subprograms. > Can you program "const correct"? Eg. if you declare a member function > as const the compiler will help you not mutate the object or call any > functions that do. Also, if you pass a parameter as a const reference, > you will not be able to mutate the object the parameter references. Ada syntax for defining what most other languages call classes is a bit unique. Ada does not use the term "class" like other languages. The Ada package is used for encapsulation and namespace. A package specification contains an implicit public part and an explicit private part. A package may contain more than one type definiton. Extensible types in Ada are called tagged types. They were introduced in Ada 95 as an extension of the Ada 83 record syntax. Ada does not use the dot notation for calling member methods for tagged types. For example, if you define a dog class in C++ or Java, and provide a feed() member function, you call the function as: dog d; d.feed(); In Ada you can create a dog tagged type, and in the same package specification you can define a procedure to feed the dog. The procedure specification would look like: procedure feed(The_Dog : out dog); The procedure would be called as: d : dog; feed(d); or, using named notation: feed(The_Dog => d); The procedure specification is the key to "const correctness". Procedure parameters have a passing mode. Three options are possible for the passing mode: IN --- Parameter is treated as a const (read-only) value within the procedure or function. Function parameters can only be IN mode. OUT --- Parameter is both readable and writeable within the procedure, but there is not guarantee of an intial value for the parameter IN OUT - Parameter is both readable and writeable within the procedure. Parameter retains the value passed in from the actual parameter. Jim Rogers