Can I restrict generic type parameters to require the Default_Value aspect?

Ada 2012 comes with a Default_Value aspect for types. Is there any way to restrict generic type parameters to require that they have a default value (or is there even a general way to check for any aspects?)

basically my question is if the following example can be made safe. currently gnat throws a warning

main.adb:6:03: warning: variable "Var" is read but never assigned [-gnatwv

when i don't define MyFloat with with Default_Value => 10.0.

-- main.adb
with MyProc;

procedure test is
  type MyFloat is new Float with Default_Value => 10.0;
  package P is new MyProc (MyFloat);
  Var : P.Bla;
  Var2 : MyFloat := P.Stuff (Var);
begin
  null;
end test;

-- MyProc.ads
generic
  type MyTypeWithDefault is private;
package MyProc is
  type Bla is tagged private;
  function Stuff (Self : Bla) return MyTypeWithDefault;
private
  type Bla is tagged record
    Data : MyTypeWithDefault;
  end record;
end MyProc;

-- MyProc.adb
package body MyProc is
  function Stuff (Self : Bla) return MyTypeWithDefault is
  begin
    return Self.Data;
  end Stuff;
end MyProc;

Answers


You could do this in pre Ada 2012 style:

generic
  type MyTypeWithDefault is private;
  Default_Value : in MyTypeWithDefault;
package MyProc is
  type Bla is tagged private;
  function Stuff (Self : Bla) return MyTypeWithDefault;
private
  type Bla is tagged record
    Data : MyTypeWithDefault:= Default_Value;
  end record;
end MyProc;

But you do raise a good point; there should be some way to specify the new aspects in the formal parameters of a generic.


Need Your Help

Build Uri of the format X:Y

android uri

I'm trying to build a Uri the most correct way for an intent to query a location on a map. In the documentation for a Maps intent, it is states that Uris should be of the form: geo:0,0?q=my+street+

How can i convert a string into a ZZ number?

c++ ntl elgamal

I'm using the NTL library to implement ElGamal encryption/decryption algorithm.