โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

How do I ensure a record's discriminant value is equal to another record component's value?

I have a File_Reader composed of two records, File and Buffer. I would like to ensure both Records always have a valid buffer size when initialized, i.e. Data_File.IO_Buffer_Size is equal to Data_In.Size.

I couldn't find a way of initializing a record component's value with another record's component value or discriminant, so I figured I would at least apply a static predicate which was unsuccessful. Using dynamic predicate as,an alternative poses issues in a LightRuntime environment.

I could easily add a Buffer_Size discriminant to File_Reader, but I would like to explore alternative solutions. Lastly, the record layouts must be preserved since they are memory mapped using representation clauses which are not shown:

type File is 
   record
      Name           : String;
      IO_Buffer_Size : Buffer_Size;
   end record; 

type Buffer(Size : Buffer_Size := 300)  is         
   record
      Pos   : Buffer_Size := 0;
      Value : String(1 .. Size);
   end record;

type File_Reader(Name : String) is 
   record        
       Data_In      : Buffer;    
       Data_File    : File := (Name, Data_In.Size); -- Won't work
   end record;

Why does elaboration require the programmers attention in Ada? [closed]

I stumbled upon an in depth explanation of elaboration, it's standardized ordering scheme, and the options available to the programmer for controlling it in unsuccessful cases.

http://www.cs.uni.edu/~mccormic/4740/documentation/elaboration

First, it's still unclear why elaboration needs to be addressed by the programmer at all. I've never dealt with it in any other language such as C, Pascal or C++ where the order of allocation and assignment variables, as well as code execution, are rarely of concern. Why isn't it automatically handled and what benefits does it offer to the developer?

Second, elaboration failures are often not detected statically, but at runtime only as a "program error" exception. I find it a bit disconcerting, considering it can suddenly occur in a number of scenarios, including changes in the runtime, the code base, the compiler or its version. It seems to belie Ada's core tenet of code safety and reliability.

How well can Ada optimize static constant arrays?

Let's say I declare an array of constant values for use as a lookup table:

function Square(num : Integer) return Integer is
use Ada.Text_IO;
type LookupItemsRange is range 1..10;
type LookupTable is array(LookupItemsRange) of integer;
A : constant LookupTable := (2312,2,3,4,5,6,7,8,9, 10);
begin
    Sqr := A(1);
    Sqr := 2 * A(2);
    return Sqr;
end Square;

The LookupTable shouldn't use any space. The compiler should be able to substitute array access with the actual inlined values. At the highest optimization setting, here's what Ada yields:

_ada_square:
        li      a0,4                #,
        ret     

I am planning on substituting a large amount of redundant MCU code with a cleaner lookup table implementation. Since the microcontroller's resources are constrained, I would like to avoid needless allocation and indexing.

Under what circumstances will Ada allocate the array or am I completely safe?

How do I define and statically initialize a vector index by an enumeration?

I'm unable to define a vector using an enumeration as an index.

First I define my record:

type contact_name is record
    first    : unbounded_string;
    last     : unbounded_string;
end record;

I define my enumeration:

type profession is (plumber, doctor, lawyer, ombudsman, dealer);

I declare my vector of contact_name using professional as the index type:

package Pro_Vector is new Ada.Containers.Vectors (Index_Type => Profession, Element_Type => contact_name);

Finally, I build my table:

Pro_Table : Pro_Vector.Vector := (plumber, ("Bob","daPlumah")) & 
(doctor, "Felix", "FeelGood"))

When I try to compile it says expect signed integer type of Index_Type. It also claims Pro_Vector is undefined. I substituted profession for natural and it compiled, but my static initialization has errors.

Why won't it accept my enum as an index. I was under the impression that Ada is super safe. By using an unconstrained type like Natural, doesn't it compromise safety. Also, how do I statically initialize my vector?

โŒ
โŒ