Live data from Hacker News

Ada for the C++ and Java Developer [pdf]

learn.adacore.com

61–70 of 72 posts

Re: Ada for the C++ and Java Developer [pdf]

#61
post #16

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

I am not an Ada expert, but I came up with this. Please notice that the idea here is not to have the compiler do proper dimensional analysis but simply to avoid the default operator overloading of "*" for the Distance type that also returns a Distance value. Nothing very high-level, as we need to explicitly manipulate the single-component records, but it does the job. with Ada.Text_IO; use Ada.Text_IO; procedure Main…

As someone said, the best solution here is to use the dimensionality analysis in GNAT: https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ug...

as in:

  with Ada.Text_IO; use Ada.Text_IO;
  with System.Dim.Float_Mks; use System.Dim.Float_Mks;
  with System.Dim.Float_Mks_IO; use System.Dim.Float_Mks_IO;

    procedure Main is
       subtype Distance is System.Dim.Float_Mks.Length;
       subtype Area is System.Dim.Float_Mks.Area;

       D1 : constant Distance := 10.0*m;

       D2 : constant Distance := 20.0*m;

       -- D3 : constant Distance := D1 * D2;
       -- Does not compile. GNAT returns the following error:
       --   main.adb:13:08: dimensions mismatch in object declaration
       --   main.adb:13:37: expected dimension [L], found [L**2]

       A : constant Area := D1 * D2;
    begin
       -- print: Area A is  2.00000E+02 m**2
       Put ("Area A is ");
       Put (Item => A);
       Put_Line ("");
    end Main;

Re: Ada for the C++ and Java Developer [pdf]

#62
post #16

Earlier quoted context omitted.

I am not an Ada expert, but I came up with this. Please notice that the idea here is not to have the compiler do proper dimensional analysis but simply to avoid the default operator overloading of "*" for the Distance type that also returns a Distance value. Nothing very high-level, as we need to explicitly manipulate the single-component records, but it does the job. with Ada.Text_IO; use Ada.Text_IO; procedure Main…

As someone said, the best solution here is to use the dimensionality analysis in GNAT: https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ug... as in: with Ada.Text_IO; use Ada.Text_IO; with System.Dim.Float_Mks; use System.Dim.Float_Mks; with System.Dim.Float_Mks_IO; use System.Dim.Float_Mks_IO; procedure Main is subtype Distance is System.Dim.Float_Mks.Length; subtype Area is System.Dim.Float_Mks.Area; D1 :…

Oh, thanks for pointing it out. Honestly, I was more interested in leveraging the type system than on proper dimensional analysis. I could find no mechanism by which Ada allows the programmer to disable the default operation overloads, so I decided to try that out.

Re: Ada for the C++ and Java Developer [pdf]

#63

Ada seems like a really good language. Is there any reason why it isn't more popular?

1. Being good is not enough to be popular. In most cases to be popular a technology needs to be actively promoted by a big brand (of at least a non-profit foundation). E. g. Java is popular thanks to Sun and Go thanks to Google. Also it helps to have free of cheap tools.

2. This problem also has another side - C is much more popular than the language itself warrants.

I see here following reasons: 1. Unix (which is popular in academia since 70s-80s) and later GCC (it was hard to compete with a free compiler at times when most other required an expensive license) 2. Microsoft designated C and C++ as "official" languages for Windows: MS provided IDE - Visual C++ supported only C/C++ [1] and official documentation implies that everybody uses C or C++ to create Windows apps.

[1] Visual Studio later added .Net support, but this not reduced C/C++ popularity because .Net competes mostly with Java.

Re: Ada for the C++ and Java Developer [pdf]

#64

Ada seems like a really good language. Is there any reason why it isn't more popular?

Ada was standard in aerospace and defence projects in the UK when I started in s/w back in the 80's, although personally never worked in those areas. It may be that its perceived lack of popularity is tied to its association with those rather more secretive lines of work, although that doesn't in and of itself explain why it didn't become more broadly used - other commenters have mentioned cost, and that consideratio…

On the other hand I really quite like programming in Postgres's version of PL/SQL which I find to be pleasantly consistent and quite easy to understand.

Re: Ada for the C++ and Java Developer [pdf]

#65
post #5

Does Ada have real memory safety features? Or is it of the "better than nothing" C++ kind? For example: are array out-of-bounds checked? Or prevented at compile time? What about overflow? ...

From the book: > dynamic checks (such as array bounds checks) provide verification that could not be done at compile time. Dynamic checks are performed at runtime, similar to what is done in Java.

Check out the table on https://docs.adacore.com/spark2014-docs/html/ug/en/usage_sce....

> SPARK builds on the strengths of Ada to provide even more guarantees statically rather than dynamically. As summarized in the following table, Ada provides strict syntax and strong typing at compile time plus dynamic checking of run-time errors and program contracts. SPARK allows such checking to be performed statically. In addition, it enforces the use of a safer language subset and detects data flow errors statically.

Re: Ada for the C++ and Java Developer [pdf]

#66

Earlier quoted context omitted.

Ada was standard in aerospace and defence projects in the UK when I started in s/w back in the 80's, although personally never worked in those areas. It may be that its perceived lack of popularity is tied to its association with those rather more secretive lines of work, although that doesn't in and of itself explain why it didn't become more broadly used - other commenters have mentioned cost, and that consideratio…

On the other hand I really quite like programming in Postgres's version of PL/SQL which I find to be pleasantly consistent and quite easy to understand.

Have only recently started with Postgres, but have been really impressed with the whole product and yes totally agree - the language seems to be designed by people who actually understand the DML tasks that programmers want to perform.

Re: Ada for the C++ and Java Developer [pdf]

#67
post #57

One of my favorite features of Ada 2012 are the design-by-contract checks taken from Eiffel[0]: Preconditions: "a condition or predicate that must always be true just prior to the execution of some section of code or before an operation"[1]. It is up to the caller (client) to set up the preconditions and ensure that they are true before calling the code in question. If any preconditions are not met, it is the fault o…

Any language that has assertions can be used to simulate at least preconditions and postconditions, and maybe some support for invariants. As the team leader for the second version of a database middleware product developed in C, being convinced by study of the efficacy of assertions for DbC and hence better quality software, I made sure my team used C assertions heavily throughout the codebase, at the entry and exit…

I should add that the product being a success wss not solely due to using assertions for DbC, of course, although, IMO, that did play a significant role in bug detection and hence removal and better product quality. I also ensured that the team consistently applied some other basic software engineering practices, which were not ordinarily followed in that company. The product success was the result of all the practices that were applied and also a good team.

Re: Ada for the C++ and Java Developer [pdf]

#68
post #62

Earlier quoted context omitted.

As someone said, the best solution here is to use the dimensionality analysis in GNAT: https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ug... as in: with Ada.Text_IO; use Ada.Text_IO; with System.Dim.Float_Mks; use System.Dim.Float_Mks; with System.Dim.Float_Mks_IO; use System.Dim.Float_Mks_IO; procedure Main is subtype Distance is System.Dim.Float_Mks.Length; subtype Area is System.Dim.Float_Mks.Area; D1 :…

Oh, thanks for pointing it out. Honestly, I was more interested in leveraging the type system than on proper dimensional analysis. I could find no mechanism by which Ada allows the programmer to disable the default operation overloads, so I decided to try that out.

IIRC you can disable/forbid the default like so:

    Function "*"(Left, Right: Distance) return Distance is abstract;
then use:

    Function "*"(Left, Right : Distance) return Area;

Re: Ada for the C++ and Java Developer [pdf]

#69
post #9

If any Ada users are here, I have question on one section: procedure Main is type Distance is new Float; type Area is new Float; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin D1 := D1 + D2; -- OK D1 := D1 + A; -- NOT OK: incompatible types for "+" operator A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment A := Area (D1 * D2); -- OK end Main; > The predefined Ada rules are not perfect; they…

I got it working with: with Ada.Text_IO; use Ada.Text_IO; procedure Main is type Distance is new Float; type Area is new Float; function "*" (Left, Right : Distance) return Area is temp : Distance; begin temp := Left * Right; return Area (temp); end "*"; D1 : Distance := 2.0; D2 : Distance := 3.0; A : Area; begin A := D1 * D2; -- OK Put_Line(A'Image); end Main; Interestingly, it took me several attempts to create thi…

IIRC, the one-liner here [assuming you disabled the default "" operator] would be:

    Return Area( Distance'Base'(Left * Right) );
The apostrophe at the inner portion is 'qualification', a method for directing the compiler that the enclosed value is supposed to be a particular subtype. (Typically used to resolve ambiguities.)

Re: Ada for the C++ and Java Developer [pdf]

#70

One of my favorite features of Ada 2012 are the design-by-contract checks taken from Eiffel[0]: Preconditions: "a condition or predicate that must always be true just prior to the execution of some section of code or before an operation"[1]. It is up to the caller (client) to set up the preconditions and ensure that they are true before calling the code in question. If any preconditions are not met, it is the fault o…

The problem I’ve seen with trying to add non-static contracts to performance-sensitive languages has been ideological rather than technical. Specifically, do the language semantics allow a mode where these checks can be turned off? Leaving them on all the time can be a huge performance burden, both directly and in the barriers they add to optimization (while there are potential optimization benefits to exploiting the…

> Specifically, do the language semantics allow a mode where these checks can be turned off? Leaving them on all the time can be a huge performance burden, both directly and in the barriers they add to optimization (while there are potential optimization benefits to exploiting the guarantees of contracts, I don’t think as a practical matter they balance out).

Yes, with Ada it is typically a pragma or compiler-flag to turn checks off; however, Ada has a long history of having mandatory-checks and strongly-encouraging compiler-implementers to optimize the checks away when it is known they cannot fail (which is a surprising chunk of the time if you're coming from a C-based language).

Post reply on HN