News for hackers. Stuff that matters.
Re:Learning Ada by gvb on Thursday March 28, 2019 @08:07AM (#19511346)
WRT "out" parameters, it means that very literally. There the variable is only passed out, it is not passed "in." Steve's test never assigns "A" in Foo() so "A" is an uninitialized variable that then gets stored in the mainline "A" (which has the same name, but is an entirely different variable). The mainline "A" is assigned the value 12 but then gets overwritten by the uninitialized value passed out by Foo().

   with Ada.Text_IO; use Ada.Text_IO;

   procedure Outp is
      procedure Foo (A : out Integer) is
      begin
         Put_Line (Integer'Image (A));
      end Foo;
      A : Integer := 12;
   begin
      Foo(A);
   end Outp;
A clearer example would be:

   with Ada.Text_IO; use Ada.Text_IO;

   procedure Outp is
      procedure Foo (A : out Integer) is
      begin
         Put_Line (Integer'Image (A)); -- Uninitialized variable, random value
         A := 42;
         Put_Line (Integer'Image (A));
      end Foo;
      A : Integer := 12;
   begin
      Put_Line (Integer'Image (A));
      Foo(A);
      Put_Line (Integer'Image (A));
   end Outp;
This will still give the uninitialized variable warning (typically can and should configure the compiler to turn that into an error, not a warning).

Playing compiler, the output should be

   12 -- Outp()
   <random value> -- Foo()
   42 -- Foo()
   42 -- Outp()
You can make variables in and out by saying that (literally). This will do what Steve expected:

   with Ada.Text_IO; use Ada.Text_IO;

   procedure Outp is
      procedure Foo (A : in out Integer) is
      begin
         Put_Line (Integer'Image (A));
      end Foo;
      A : Integer := 12;
   begin
      Foo(A);
   end Outp;
Re:Learning Ada by 0xDEEPFAC on Thursday March 28, 2019 @09:22AM (#19511895)
Also, added in a recent version of the language (as part of 2020) you can simply use 'Image directly on an object saving you a bit of typing : ). And let me let you in on a secret Ada programmers don't talk about or put in examples - you don't have to type the Identifier at the end of the procedures!

   with Ada.Text_IO; use Ada.Text_IO;
   procedure Outp is
      procedure Foo (A : in out Integer) is
      begin
         Put_Line (A'Image);
      end;
      A : Integer := 12;
   begin
      Foo (A);
   end;
Re:Learning Ada by thestoicattack on Thursday March 28, 2019 @09:25AM (#19511922)
Ooh, I like that new 'Image usage.

I was told that adding the identifier to the end of blocks is just a good practice, since the compiler can tell you if you misread the nesting and wrote the wrong name.

Re:Learning Ada by gliptic on Thursday March 28, 2019 @09:34AM (#19512010)
I think Steve is reacting more to this not being a compile error.
Re:Learning Ada by steveklabnik on Thursday March 28, 2019 @10:06AM (#19512282)
Yes, the text explained how I should write this, my surprise is that I can get undefined behavior. And that the text says that this may, but is not guaranteed to, warn.

On Twitter the other day, someone suggested that this is because, when Ada was first made, this kind of analysis was truly to expensive, and so the guarantee was not made, and they're sticking to backwards compatibility, and modern implementation should basically always warn. That seems reasonable, but I have no idea if it's correct. I assume most real-world Ada scenarios treat warnings as errors.

Re:Learning Ada by sanxiyn on Thursday March 28, 2019 @10:58AM (#19512792)
Yes, the analysis is called definite assignment analysis: https://en.wikipedia.org/wiki/Definite_assignment_analysis

It was a great victory of Java and C# to require this. Java's analysis is "buggy" and C#'s is complete.

Re:Learning Ada by twic on Thursday March 28, 2019 @02:40PM (#19515202)
To be clear, buggy in that it can sometimes not detect correct assignment, and require you to add some unnecessary assignments to shut it up. Not in that you can ever read an uninitialised variable.

That is a bit of a theme in Java. The type inference often has the same problem.

(Although the type inference is also actually unsound)

Re:Learning Ada by kqr on Sunday March 31, 2019 @03:53PM (#19537934)
Hub, is that supposed to not be the case with C#? Because I've definitely had that experience at least once, of knowing a variable is initialized and the compiler not "getting" it until I added some semantically redundant formality.
Re:Learning Ada by galangalalgol on Thursday March 28, 2019 @11:20AM (#19513052)
It had processing constraints and it probably lacked some too like c interop. But largely it has the same target as rust right? Back when rust first got started was there someone on the team that knew Ada well in order to take lessons learned. It seems kind of crazy if there wasn't. It would be like Tesla not having anyone from a traditional car company involved in initial design.
Re:Learning Ada by steveklabnik on Thursday March 28, 2019 @11:37AM (#19513237)
In general, I have a rule of thumb: if the question is "does Graydon know about $LANGUAGE", the answer is almost always "yes." I haven't specifically spoken to him about it, though.
Re:Learning Ada by coldtea on Thursday March 28, 2019 @06:48PM (#19517423)
>Back when rust first got started was there someone on the team that knew Ada well in order to take lessons learned. It seems kind of crazy if there wasn't. It would be like Tesla not having anyone from a traditional car company involved in initial design.

Or like Go only taking into account C, Limbo, and CSP.

Re:Learning Ada by pmderodat on Thursday March 28, 2019 @01:10PM (#19514237)
It’s a good opportunity to point that Ada defines “bounded errors” and “erroneous execution” as distinct things.

TLDR: reading uninitialized data in Ada is safer than in C.

Reading an uninitialized OUT argument is a bounded error, which is much less dramatic than C’s undefined behavior: the semantics is that you’ll read an arbitrary value (which is obviously bad as far an program intent is concerned), but otherwise the language constraints still hold.

This is very different from erroneous execution, which is very similar to the famous undefined behaviors in C, in which all bets are off and anything can happen.

Re:Learning Ada by steveklabnik on Thursday March 28, 2019 @01:14PM (#19514299)
Ah thank you! As I dig into the reference material, I’m sure I’ll gain this kind of nuance, but I appreciate you pointing this out! That indeed does also make it less dangerous.