comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: passing parametars as determinants
Date: Wed, 22 Oct 2014 10:25:09 -0700 (PDT)
Date: 2014-10-22T10:25:09-07:00	[thread overview]
Message-ID: <5c9aa35f-c12e-4212-b8b9-4d4c6340302a@googlegroups.com> (raw)
In-Reply-To: <a3ba492e-7ef4-4ebb-b2fb-0f4b668a2ad3@googlegroups.com>

On Tuesday, October 21, 2014 11:11:58 PM UTC-7, mockturtle wrote:

> As far as I know, not directly.  You have two alternatives:
>   1.  Pass c as an access to string. 
>   2.  Give to the task an entry point Init (or something similar) and use it to pass c.  Something like

>     task body myTaskType is
>        local_copy : myOtherType;
>     begin
>       accept Init(c: myOtherType) do
>          local_copy := c;
>       end Init;
>       -- Back to serious work
>     end myTaskType;

> Why are not we allowed to use generic types as discriminants?  Honestly, I do not know, I have always been curious... 

Do you really mean "generic", or do you mean "general"?  Generic formal types can be used as discriminants, as long as they're known to be discrete:

    generic
        type T is (<>);
    package Pack1 is
        task type Task_Type (Discr : T) is ...  -- legal

As for why discriminant types are limited to discrete and access types: In Ada 83, discriminants were allowed only on record types (not tasks), and there were no access discriminants.  The main use of discriminants on a record type were to provide a bound for an array component:

    subtype String_Length is Integer range 0 .. 1000;
    type Varying_String (Length : String_Length := 0) is record
        Data : String (1 .. Length);
    end record;

or to use in a variant part:

    type Variant_Record (D : Data_Type) is record
        case D is
            when Integer_Type =>
                Int_Data : Integer;
            when Float_Type =>
                Float_Data : Float;
            when Boolean_Type =>
                Boolean_Data : Boolean;
        end case;
    end record;

For both of these purposes, only discrete types make sense as discriminants.

For task discriminants, which were added in Ada 95, a String discriminant (parameter) would pose a problem if you used a local variable as a parameter:

    type Task_Type (Data : String) is
        ...
    end Task_Type;
    type Task_Type_Acc is access all Task_Type;

    procedure Proc is
        Task_Data : constant String := Some_Function_Call;
        New_Task  : Task_Type_Acc := new Task_Type (Task_Data);
    begin
        ...
    end Proc;

The task could outlive Task_Data, causing a problem.  Discrete discriminants don't have that problem because they're copy-in, and access discriminants don't have that problem because the accessibility rules prevent the discriminant from being the 'Access of something that won't live as long as the task.  (Of course you could defeat it with 'Unchecked_Access, or by passing an access to an allocated object and then using Unchecked_Deallocation on the object.)  I guess you could legitimately ask why they didn't allow fixed-point or floating-point types, which are copy-in, to be used as discriminants.

                                -- Adam


      parent reply	other threads:[~2014-10-22 17:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-22  5:12 passing parametars as determinants compguy45
2014-10-22  5:16 ` compguy45
2014-10-22  5:22 ` Simon Wright
2014-10-22 11:55   ` compguy45
2014-10-22 12:39     ` Dennis Lee Bieber
2014-10-22 15:57     ` Shark8
2014-10-22  6:11 ` mockturtle
2014-10-22  8:51   ` Georg Bauhaus
2014-10-22 17:25   ` Adam Beneschan [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox