comp.lang.ada
 help / color / mirror / Atom feed
* passing parametars as determinants
@ 2014-10-22  5:12 compguy45
  2014-10-22  5:16 ` compguy45
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: compguy45 @ 2014-10-22  5:12 UTC (permalink / raw)


I have task type such as...

task type myTaskType (a: Integer; b: Integer);

I have type.....

type myOtherType is array (1 ..N-1) of String (1..N);

I would like to pass array of myOtherType into my task type declaration like this...

task type myTaskType (a: Integer; b: Integer; c : myOtherType); ??

Is there way to do this?




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  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  6:11 ` mockturtle
  2 siblings, 0 replies; 9+ messages in thread
From: compguy45 @ 2014-10-22  5:16 UTC (permalink / raw)


i was also trying to say i wanted to pass this as parametar when i am creating tasks dynamically using new keyword


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  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  6:11 ` mockturtle
  2 siblings, 1 reply; 9+ messages in thread
From: Simon Wright @ 2014-10-22  5:22 UTC (permalink / raw)


compguy45@gmail.com writes:

> I have task type such as...
>
> task type myTaskType (a: Integer; b: Integer);
>
> I have type.....
>
> type myOtherType is array (1 ..N-1) of String (1..N);
>
> I would like to pass array of myOtherType into my task type
> declaration like this...
>
> task type myTaskType (a: Integer; b: Integer; c : myOtherType); ??
>
> Is there way to do this?

ARM 3.7(1)[1] says "A discriminant of an object is a component of the
object, and is either of a discrete type or an access type."

An array is not a discrete type: but you can use an access-to-array,

   task type myTaskType (a: Integer; b: Integer; c : access myOtherType);

[1] http://www.ada-auth.org/standards/12rm/html/RM-3-7.html#p1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  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  6:11 ` mockturtle
  2014-10-22  8:51   ` Georg Bauhaus
  2014-10-22 17:25   ` Adam Beneschan
  2 siblings, 2 replies; 9+ messages in thread
From: mockturtle @ 2014-10-22  6:11 UTC (permalink / raw)


On Wednesday, October 22, 2014 7:12:56 AM UTC+2, comp...@gmail.com wrote:
> I have task type such as...
> 
> 
> 
> task type myTaskType (a: Integer; b: Integer);
> 
> 
> 
> I have type..... 
> 
> type myOtherType is array (1 ..N-1) of String (1..N);
> 
> I would like to pass array of myOtherType into my task type declaration like this...
> 
> task type myTaskType (a: Integer; b: Integer; c : myOtherType); ??
> 
> Is there way to do this?

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...  Note, however, that the task "parameters" are actually called "discriminants," so I guess they are closer (in some sense) to the record discriminants.

Riccardo


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  2014-10-22  6:11 ` mockturtle
@ 2014-10-22  8:51   ` Georg Bauhaus
  2014-10-22 17:25   ` Adam Beneschan
  1 sibling, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2014-10-22  8:51 UTC (permalink / raw)


On 22.10.14 08:11, mockturtle wrote:
> Why are not we allowed to use generic types as discriminants?  Honestly, I do not know, I have always been curious...  Note, however, that the task "parameters" are actually called "discriminants," so I guess they are closer (in some sense) to the record discriminants.

To make composite objects accessible to a task use visibility.
They are not like discriminants used to create variant parts,
and can be used more freely:

package Run_Task is
    
    type Text is access String;
    type Place is range 0 .. 99;
    type Data is array (Place range <>) of Text;

    procedure Run_It (Env : in out Data);
    
end Run_Task;

with Ada.Text_Io;
package body Run_Task is
    
    procedure Run_It (Env : in out Data)
    is
       task type My_Task (A, B: Place) is
          entry Go;
       end My_task;
       
       task body My_Task is
       begin
          accept Go;
          for K in Place range A .. B loop
             declare
                Line : Text renames Env (K);
             begin
                Line (Line'First) := '\';
                Line (Line'Last) := '_';
             end;
          end loop;
       end My_Task;
       
       T1 : My_Task (A => Place'First,
                     B => (Place'Last - Place'First) / 2);
       T2 : My_Task (A => (Place'Last - Place'First) / 2 + 1,
                     B => Place'Last);
    begin  -- Run_It
       T1.Go;
       T2.Go;
    end Run_It;
    
    Panel : Data := (Place'range => new String'(1 .. 80 => '*'));
begin
    Run_It (Env => Panel);
    for Row in Panel'Range loop
       Ada.Text_Io.Put_Line (Panel (Row).all);
    end loop;
end Run_Task;


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  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
  0 siblings, 2 replies; 9+ messages in thread
From: compguy45 @ 2014-10-22 11:55 UTC (permalink / raw)


task type myTaskType (a: Integer; b: Integer; c: access myOtherType); 


This is where i create tasks.....


:= new myTaskType(a=> i, b => j,c =>  access'myOtherType);

I get compiler error "reserved word "access" cannot be used as identifier"


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  2014-10-22 11:55   ` compguy45
@ 2014-10-22 12:39     ` Dennis Lee Bieber
  2014-10-22 15:57     ` Shark8
  1 sibling, 0 replies; 9+ messages in thread
From: Dennis Lee Bieber @ 2014-10-22 12:39 UTC (permalink / raw)


On Wed, 22 Oct 2014 04:55:59 -0700 (PDT), compguy45@gmail.com declaimed the
following:

>task type myTaskType (a: Integer; b: Integer; c: access myOtherType); 
>
>
>This is where i create tasks.....
>
>
>:= new myTaskType(a=> i, b => j,c =>  access'myOtherType);
>
>I get compiler error "reserved word "access" cannot be used as identifier"

	Try 			myOtherTypeInstance'access		instead
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  2014-10-22 11:55   ` compguy45
  2014-10-22 12:39     ` Dennis Lee Bieber
@ 2014-10-22 15:57     ` Shark8
  1 sibling, 0 replies; 9+ messages in thread
From: Shark8 @ 2014-10-22 15:57 UTC (permalink / raw)


On 10/22/2014 5:55 AM, compguy45@gmail.com wrote:
> task type myTaskType (a: Integer; b: Integer; c: access myOtherType);
>
>
> This is where i create tasks.....
>
>
> := new myTaskType(a=> i, b => j,c =>  access'myOtherType);
>
> I get compiler error "reserved word "access" cannot be used as identifier"
>

Here's an example of using an access discriminant in a task-type:

With
Ada.Text_IO;

procedure Test is

    -- This is the type for the data to be passed to the task-type.
    Type Info is array (positive range <>) of Integer;

    -- The task-type's public interface.
    Task Type Print_Task( Info_Param : not null access Info ) is
       entry Print  ( Index : in Positive );
       entry Done;
    end Print_Task;


    -- The body of the task-type.
    Task body Print_Task is
       subtype Data_Range is Positive range Info_Param'Range;
       Procedure Put( Index : Data_Range ) is
       begin
          Ada.Text_IO.Put_Line("Data(" & Integer'Image(Index) & " ) ->"
                               & ASCII.HT & Integer'Image(Info_Param(Index))
                              );
       end Put;
       -- Variable indicating if we're finished.
       Finished : Boolean := False;
    begin
       loop
          -- Select lets us accept things non-sequentially; A or B,
          -- rather than A then B.
          select
             accept Done  do
                -- We're free to exit the task.
                Finished:= True;
             end Done;
          or
             accept Print (Index : in Positive) do
                -- If the input is outside the range of the data, we print
                -- an error-message, otherwise we desplay the indicated 
data.
                if Index not in Data_Range then
                   Ada.Text_IO.Put_Line("[ERROR] Index out of range.");
                else
                   Put(Index);
                end if;
             end Print;
          end select;
          -- When Finished is flagged, we break out of the loop.
          exit when Finished;
       end loop;
       Ada.Text_IO.Put_Line("Goodbye.");
    end Print_Task;


    -- This is the actual object of data we will pass to the task.
    Data : aliased Info:= (8,3,-1,2);

    -- The actual task.
    SOME_TASK : Print_Task( Info_Param => Data'Access );



    use Ada.Text_IO;
begin
    SOME_TASK.Print(3);
    SOME_TASK.Done;
--     Put_Line( "P(24) =" & P(K'Last)'Img );
end Test;

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: passing parametars as determinants
  2014-10-22  6:11 ` mockturtle
  2014-10-22  8:51   ` Georg Bauhaus
@ 2014-10-22 17:25   ` Adam Beneschan
  1 sibling, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2014-10-22 17:25 UTC (permalink / raw)


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-10-22 17:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox