โŒ About FreshRSS

Normal view

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

Access-to-variable designates constant when trying to pass a pointer to vector element

This question is a follow-up to How to get access to a record field .

The more complex (now) code involves binding a whole vector of records. Something along the lines below:

type Tag is record
  Field : String := "value";
end record;

type Tag_Access is access all Tag;

package Tags_Vectors is new Indefinite_Vectors
  (Index_Type   => Positive,
   Element_Type => Wheel_Tag);

procedure Bind_Tag (T : in out Tag_Access; Stmt : Gnade.Statement) is
begin
    Gnade.Bind_Text (Stmt, T.Field'Address, T.Field'Length);
end Bind_Tag;

procedure Insert_Tags is
  Stmt : Gnade.Statement;
  Tags : Tags_Vectors.Vector;
  --  Make necessary arrangements to populate the Stmt and Tags
  C : Tags_Vectors.Cursor := Tags.First;
begin
  while Tags_Vectors.Has_Element (C) loop
         Bind_Tag (Stmt, Tags_Vectors.Element (C)'Access);
         Tags_Vectors.Next (C);
  end loop;
  Gnade.Step (Db, Stmt);
end Insert_Tag;

I'm not sure what kind of thing Tags_Vector.Element (C) returns. Syntactically, at least, it seems Ada doesn't object to this having an access attribute. But, I don't understand the error (which side does it think is the variable and which side is the constant?) Why is it bad that access to variable designates a constant? (Is it trying to say that I might be changing the value of a constant? -- but I never wanted any of those things to be constants...)

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?

โŒ
โŒ