Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

generics - Ada dynamic array instantiation

Building on this instructive question, I'm having difficulty instantiating the array:

DynamicArrays.ads

    generic
        type Value_Type is private;
        
    package DynamicArrays is

        type Dynamic_Array is tagged private;

        function Length (Self : Dynamic_Array) return Natural with Inline;

    private
        type Static_Array is array (Positive range <>) of aliased Value_Type;
        type Static_Array_Access is access all Static_Array;

        type Dynamic_Array is tagged record
            Backing : Static_Array_Access;
        end record with
          Variable_Indexing => Variable_Indexer;

        type Variable_Reference (Data : access Value_Type) is null record with
          Implicit_Dereference => Data;
        
        function Variable_Indexer (Self : in out  Dynamic_Array; Indexer : Positive)
                                         return Variable_Reference with Pre => Indexer <= Self.Length;
    end DynamicArrays;

DynamicArrays.adb

    package body DynamicArrays is

        function Variable_Indexer (Self : in out Dynamic_Array; Indexer : Positive) return Variable_Reference is
        begin
            return Variable_Reference'(Data => Self.Backing (Index)'Access);
        end Variable_Indexer;
        
    end DynamicArrays;

Main.adb

    with Ada.Text_IO; use Ada.Text_IO;
    with DynamicArrays;
    procedure Main is

        --! 5:9 Indexing aspect cannot be specified on full view if partial view is tagged
        --! 5:9 instantiation error at dynamicarrays.ads:21
        package IntegerDA is new DynamicArrays (Value_Type => Integer);
        use IntegerDA;

        --! 7:14 subtype mark required in this context
        DA : Integerda := 'a' & 'b' & 'c';
        
    begin
        --! 10:24 invalid prefix in selected component "DA"
        pragma Assert (DA.Length = 3);
        pragma Assert (DA (1) = 'a');
        pragma Assert (DA (2) = 'b');
        pragma Assert (DA (3) = 'c');
    end Main;

Exclamation marks => the compiler's errors (Gnat 2020).

I realise that I'm a noob at Ada, any suggestions would be greatly appreciated.

question from:https://stackoverflow.com/questions/66049769/ada-dynamic-array-instantiation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...