comp.lang.ada
 help / color / mirror / Atom feed
* Problem Eliminating constructors
@ 2003-01-17  0:19 Victor Porton
  2003-01-17 15:21 ` John English
                   ` (6 more replies)
  0 siblings, 7 replies; 15+ messages in thread
From: Victor Porton @ 2003-01-17  0:19 UTC (permalink / raw)


package A is

  type A_Type is tagged
    record
      X: Integer;
    end;
    
  function Create(X: Integer) return A_Type; -- guess what is the body
  
end A;

Then I have

type B_Type is new A.A_Type with
  record
    Y: Integer;
  end;

Compiler requires me to override Create as the return type changes.
But it is meaningless as now I need Create with two parameters
instead of one.

What to do?

Maybe there are a wau to make "function Create(X: Integer) return 
A_Type" not a primitive operation so that compiler will not more
request to override it?

Or maybe just move declaration of this func out of package A?



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

* Re: Problem Eliminating constructors
@ 2003-01-17  6:54 Grein, Christoph
  0 siblings, 0 replies; 15+ messages in thread
From: Grein, Christoph @ 2003-01-17  6:54 UTC (permalink / raw)
  To: comp.lang.ada

> package A is
> 
>   type A_Type is tagged
>     record
>       X: Integer;
>     end;
>     
    package Avoid_Primitiveness is
>     function Create(X: Integer) return A_Type; -- guess what is the body
    end Avoid_Primitiveness;
>   
> end A;
> 
> Then I have
> 
> type B_Type is new A.A_Type with
>   record
>     Y: Integer;
>   end;
> 
> Compiler requires me to override Create as the return type changes.
> But it is meaningless as now I need Create with two parameters
> instead of one.
> 
> What to do?
> 
> Maybe there are a wau to make "function Create(X: Integer) return 
> A_Type" not a primitive operation so that compiler will not more
> request to override it?
> 
> Or maybe just move declaration of this func out of package A?



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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
@ 2003-01-17 15:21 ` John English
  2003-01-17 21:10 ` Victor Porton
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: John English @ 2003-01-17 15:21 UTC (permalink / raw)


Victor Porton wrote:
> 
> package A is
> 
>   type A_Type is tagged
>     record
>       X: Integer;
>     end;
> 
>   function Create(X: Integer) return A_Type; -- guess what is the body
> 
> end A;
> 
> Then I have
> 
> type B_Type is new A.A_Type with
>   record
>     Y: Integer;
>   end;
> 
> Compiler requires me to override Create as the return type changes.
> But it is meaningless as now I need Create with two parameters
> instead of one.
> 
> What to do?

Take Create out of package A and make it a child of the package:

  function A.Create(X: Integer) return A.A_Type is ...;

Now Create will no longer be a primitive operation of A_Type since
it isn't declared in the same package spec, but you can refer to
it in exactly the same way (except you would need "with A.Create"
to be able to call it).

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------



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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
  2003-01-17 15:21 ` John English
@ 2003-01-17 21:10 ` Victor Porton
  2003-01-18 15:19 ` Simon Wright
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Victor Porton @ 2003-01-17 21:10 UTC (permalink / raw)


In article <3E281F79.A309AD5E@brighton.ac.uk>,
	John English <je@brighton.ac.uk> writes:
> Victor Porton wrote:
>> 
>> package A is
>> 
>>   type A_Type is tagged
>>     record
>>       X: Integer;
>>     end;
>> 
>>   function Create(X: Integer) return A_Type; -- guess what is the body
>> 
>> end A;

[skip]

>> Compiler requires me to override Create as the return type changes.
>> But it is meaningless as now I need Create with two parameters
>> instead of one.
>> 
>> What to do?
> 
> Take Create out of package A and make it a child of the package:
> 
>   function A.Create(X: Integer) return A.A_Type is ...;
> 
> Now Create will no longer be a primitive operation of A_Type since
> it isn't declared in the same package spec, but you can refer to
> it in exactly the same way (except you would need "with A.Create"
> to be able to call it).

Also it does not work if A is a subpackage (as it really is). I would 
need also to make A a child package instead what would force me to do 
complete restructurization of the packages tree.



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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
  2003-01-17 15:21 ` John English
  2003-01-17 21:10 ` Victor Porton
@ 2003-01-18 15:19 ` Simon Wright
  2003-01-20 11:39 ` Rodrigo García
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Simon Wright @ 2003-01-18 15:19 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> package A is
> 
>   type A_Type is tagged
>     record
>       X: Integer;
>     end;
>     
>   function Create(X: Integer) return A_Type; -- guess what is the body

Could you make Create return A_Type'Class? (it means you probably have
to initialize at creation:

  declare
     My_B : A.A_Type'Class := B.Create (...);

> end A;



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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
                   ` (2 preceding siblings ...)
  2003-01-18 15:19 ` Simon Wright
@ 2003-01-20 11:39 ` Rodrigo García
  2003-01-20 21:07 ` Victor Porton
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 15+ messages in thread
From: Rodrigo García @ 2003-01-20 11:39 UTC (permalink / raw)


Victor Porton wrote:
> package A is
> 
>   type A_Type is tagged
>     record
>       X: Integer;
>     end;
>     
>   function Create(X: Integer) return A_Type; -- guess what is the body
>   
> end A;
> 
> Then I have
> 
> type B_Type is new A.A_Type with
>   record
>     Y: Integer;
>   end;
> 
> Compiler requires me to override Create as the return type changes.
> But it is meaningless as now I need Create with two parameters
> instead of one.
> 
> What to do?

   You do not need a constructor for this simple case. This will create 
an A_Type object and initialize X to value 5.

   declare
      My_A : A.A_Type := (X => 5);

Similarly, this will create a B_Type object with X=5 and Y=6.

   declare
      My_B : B_Type := (X => 5, Y => 6);

Alternatively, you can use discriminants as well:

   type A_Type (I : Integer) is tagged
      record
         X : Integer := I;
      end record;

And then:

   type B_Type (J : Integer; K : Integer) is new A_Type (I => J) with
      record
        Y : Integer := K;
      end record;

With this solution, you are forced to provide initial values in the 
creation.

Rodrigo




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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
                   ` (3 preceding siblings ...)
  2003-01-20 11:39 ` Rodrigo García
@ 2003-01-20 21:07 ` Victor Porton
  2003-01-21  9:44   ` Dmitry A. Kazakov
                     ` (2 more replies)
  2003-01-21 13:09 ` Victor Porton
  2003-01-21 18:38 ` Victor Porton
  6 siblings, 3 replies; 15+ messages in thread
From: Victor Porton @ 2003-01-20 21:07 UTC (permalink / raw)


In article <3E2BDFD8.4070805@epfl.ch>,
	Rodrigo Garc�a <rodrigo.garcia@epfl.ch> writes:
> Victor Porton wrote:
>> package A is
>> 
>>   type A_Type is tagged
>>     record
>>       X: Integer;
>>     end;
>>     
>>   function Create(X: Integer) return A_Type; -- guess what is the body
>>   
>> end A;
>> 
>> Then I have
>> 
>> type B_Type is new A.A_Type with
>>   record
>>     Y: Integer;
>>   end;
>> 
>> Compiler requires me to override Create as the return type changes.
>> But it is meaningless as now I need Create with two parameters
>> instead of one.
>> 
>> What to do?
> 
>    You do not need a constructor for this simple case. This will create 
> an A_Type object and initialize X to value 5.
> 
>    declare
>       My_A : A.A_Type := (X => 5);
[skip]

Oh, Rodrigo, I oversimplified the real situation. Initialization with
just aggregates is possible only in the example, really I need namely
constructor functions.

Well, currently I switched to initialization by calling a procedure.

It would be nice if Ada0X would allow to make a primitive operation
non-virtual.

E.g. we can introduce "pragma Freeze(TYPE)".



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

* Re: Problem Eliminating constructors
  2003-01-20 21:07 ` Victor Porton
@ 2003-01-21  9:44   ` Dmitry A. Kazakov
  2003-01-21 19:01     ` Martin Krischik
  2003-01-21 11:43   ` Rodrigo García
  2003-01-21 15:03   ` Stephen Leake
  2 siblings, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2003-01-21  9:44 UTC (permalink / raw)


On Tue, 21 Jan 2003 02:07:29 +0500, porton@ex-code.com (Victor Porton)
wrote:

>In article <3E2BDFD8.4070805@epfl.ch>,
>	Rodrigo Garc�a <rodrigo.garcia@epfl.ch> writes:
>> Victor Porton wrote:
>>> package A is
>>> 
>>>   type A_Type is tagged
>>>     record
>>>       X: Integer;
>>>     end;
>>>     
>>>   function Create(X: Integer) return A_Type; -- guess what is the body
>>>   
>>> end A;
>>> 
>>> Then I have
>>> 
>>> type B_Type is new A.A_Type with
>>>   record
>>>     Y: Integer;
>>>   end;
>>> 
>>> Compiler requires me to override Create as the return type changes.
>>> But it is meaningless as now I need Create with two parameters
>>> instead of one.
>>> 
>>> What to do?
>> 
>>    You do not need a constructor for this simple case. This will create 
>> an A_Type object and initialize X to value 5.
>> 
>>    declare
>>       My_A : A.A_Type := (X => 5);
>[skip]
>
>Oh, Rodrigo, I oversimplified the real situation. Initialization with
>just aggregates is possible only in the example, really I need namely
>constructor functions.

Ada has constructor function. IMO what it does need is user-defined
contructors. However, most of Ada people do not share my opinion. In
any case (your statement below) constructors always *covariant*.

>Well, currently I switched to initialization by calling a procedure.
>
>It would be nice if Ada0X would allow to make a primitive operation
>non-virtual.

It is pretty dangerous, so there has to be a strong rationale for this
(i.e. to allow contravariant parameters). In your case, you could make
Create class-wide, if you want to overload it:

 function Create (X : Integer) return A_Type'Class;
   -- creates A_Type

function Create (X, Y : Integer) return B_Type'Class;
   -- creates B_Type

---
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de



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

* Re: Problem Eliminating constructors
  2003-01-20 21:07 ` Victor Porton
  2003-01-21  9:44   ` Dmitry A. Kazakov
@ 2003-01-21 11:43   ` Rodrigo García
  2003-01-21 15:03   ` Stephen Leake
  2 siblings, 0 replies; 15+ messages in thread
From: Rodrigo García @ 2003-01-21 11:43 UTC (permalink / raw)


Victor Porton wrote:
> In article <3E2BDFD8.4070805@epfl.ch>,
> 	Rodrigo Garc�a <rodrigo.garcia@epfl.ch> writes:
> 
>>Victor Porton wrote:
>>
>>>package A is
>>>
>>>  type A_Type is tagged
>>>    record
>>>      X: Integer;
>>>    end;
>>>    
>>>  function Create(X: Integer) return A_Type; -- guess what is the body
>>>  
>>>end A;
>>>
>>>Then I have
>>>
>>>type B_Type is new A.A_Type with
>>>  record
>>>    Y: Integer;
>>>  end;
>>>
>>>Compiler requires me to override Create as the return type changes.
>>>But it is meaningless as now I need Create with two parameters
>>>instead of one.
>>>
>>>What to do?
>>
>>   You do not need a constructor for this simple case. This will create 
>>an A_Type object and initialize X to value 5.
>>
>>   declare
>>      My_A : A.A_Type := (X => 5);
> 
> [skip]
> 
> Oh, Rodrigo, I oversimplified the real situation. Initialization with
> just aggregates is possible only in the example, really I need namely
> constructor functions.

   I see... Have you tried with controlled types?

Rodrigo




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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
                   ` (4 preceding siblings ...)
  2003-01-20 21:07 ` Victor Porton
@ 2003-01-21 13:09 ` Victor Porton
  2003-01-22 15:26   ` Rodrigo García
  2003-01-21 18:38 ` Victor Porton
  6 siblings, 1 reply; 15+ messages in thread
From: Victor Porton @ 2003-01-21 13:09 UTC (permalink / raw)


In article <3E2D3245.2060903@epfl.ch>,
	Rodrigo Garc�a <rodrigo.garcia@epfl.ch> writes:
>>>Victor Porton wrote:
>>>
>> Oh, Rodrigo, I oversimplified the real situation. Initialization with
>> just aggregates is possible only in the example, really I need namely
>> constructor functions.
> 
>    I see... Have you tried with controlled types?

Do you mean to use type discriminants by Initialize procedure?

I suspect this is a size overhead in current compilers (Gnat 3.14/3.15 
for me).



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

* Re: Problem Eliminating constructors
  2003-01-20 21:07 ` Victor Porton
  2003-01-21  9:44   ` Dmitry A. Kazakov
  2003-01-21 11:43   ` Rodrigo García
@ 2003-01-21 15:03   ` Stephen Leake
  2 siblings, 0 replies; 15+ messages in thread
From: Stephen Leake @ 2003-01-21 15:03 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> It would be nice if Ada0X would allow to make a primitive operation
> non-virtual.
> 
> E.g. we can introduce "pragma Freeze(TYPE)".

How is this better than a nested package?

package Foo is
   type Base is tagged private;

   package Constructors is
       function New_Base (Params : in ...) return Base;
   end Constructors;

private
   type Base is ...;

end Foo;

'New_Base' is not a primitive operation of Base, therefore it is not
dispatching.

I'm not clear why you want a subprogram that is primitive but not
dispatching. 

-- 
-- Stephe



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

* Re: Problem Eliminating constructors
  2003-01-17  0:19 Victor Porton
                   ` (5 preceding siblings ...)
  2003-01-21 13:09 ` Victor Porton
@ 2003-01-21 18:38 ` Victor Porton
  2003-01-23 17:08   ` Stephen Leake
  6 siblings, 1 reply; 15+ messages in thread
From: Victor Porton @ 2003-01-21 18:38 UTC (permalink / raw)


In article <u8yxe5yv2.fsf@nasa.gov>,
	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
> porton@ex-code.com (Victor Porton) writes:
> 
>> It would be nice if Ada0X would allow to make a primitive operation
>> non-virtual.
>> 
>> E.g. we can introduce "pragma Freeze(TYPE)".
> 
> How is this better than a nested package?
> 
> package Foo is
>    type Base is tagged private;
> 
>    package Constructors is
>        function New_Base (Params : in ...) return Base;
>    end Constructors;
> 
> private
>    type Base is ...;
> 
> end Foo;

Very simple: I more like syntax without this additional package
as it is antinatural.



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

* Re: Problem Eliminating constructors
  2003-01-21  9:44   ` Dmitry A. Kazakov
@ 2003-01-21 19:01     ` Martin Krischik
  0 siblings, 0 replies; 15+ messages in thread
From: Martin Krischik @ 2003-01-21 19:01 UTC (permalink / raw)


On Tue, 21 Jan 2003 10:44:37 +0100, Dmitry A. Kazakov wrote:

> It is pretty dangerous, so there has to be a strong rationale for this
> (i.e. to allow contravariant parameters). 

True. The amount of times where a missing "virtual" drove me insane in C++
is uncounted.

With Regards

Martin

-- 
Martin Krischik
mailto://Martin@krischik.com
http://www.krischik.com




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

* Re: Problem Eliminating constructors
  2003-01-21 13:09 ` Victor Porton
@ 2003-01-22 15:26   ` Rodrigo García
  0 siblings, 0 replies; 15+ messages in thread
From: Rodrigo García @ 2003-01-22 15:26 UTC (permalink / raw)


Victor Porton wrote:
> In article <3E2D3245.2060903@epfl.ch>,
> 	Rodrigo Garc�a <rodrigo.garcia@epfl.ch> writes:
> 
>>>>Victor Porton wrote:
>>>>
>>>
>>>Oh, Rodrigo, I oversimplified the real situation. Initialization with
>>>just aggregates is possible only in the example, really I need namely
>>>constructor functions.
>>
>>   I see... Have you tried with controlled types?
> 
> 
> Do you mean to use type discriminants by Initialize procedure?

   Yes, that's the idea.

> I suspect this is a size overhead in current compilers (Gnat 3.14/3.15 
> for me).

   Well, I implemented our simple example with and without controlled 
types and it is not a significant overhead.

   In my opinion, the use of controlled types is the only "standard" 
(and the cleanest) way that Ada proposes for constructors/destructors.

Rodrigo




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

* Re: Problem Eliminating constructors
  2003-01-21 18:38 ` Victor Porton
@ 2003-01-23 17:08   ` Stephen Leake
  0 siblings, 0 replies; 15+ messages in thread
From: Stephen Leake @ 2003-01-23 17:08 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) writes:

> In article <u8yxe5yv2.fsf@nasa.gov>,
> 	Stephen Leake <Stephen.A.Leake@nasa.gov> writes:
> > porton@ex-code.com (Victor Porton) writes:
> > 
> >> It would be nice if Ada0X would allow to make a primitive operation
> >> non-virtual.
> >> 
> >> E.g. we can introduce "pragma Freeze(TYPE)".
> > 
> > How is this better than a nested package?
> > 
> > package Foo is
> >    type Base is tagged private;
> > 
> >    package Constructors is
> >        function New_Base (Params : in ...) return Base;
> >    end Constructors;
> > 
> > private
> >    type Base is ...;
> > 
> > end Foo;
> 
> Very simple: I more like syntax without this additional package
> as it is antinatural.

It's perfectly "natural" in Ada, since it is the only way to do it. It
may not feel "natural" if you are used to some other language.

Is there any other problem with it, that "pragma Freeze (Type)" would
fix? 

-- 
-- Stephe



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

end of thread, other threads:[~2003-01-23 17:08 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-17  6:54 Problem Eliminating constructors Grein, Christoph
  -- strict thread matches above, loose matches on Subject: below --
2003-01-17  0:19 Victor Porton
2003-01-17 15:21 ` John English
2003-01-17 21:10 ` Victor Porton
2003-01-18 15:19 ` Simon Wright
2003-01-20 11:39 ` Rodrigo García
2003-01-20 21:07 ` Victor Porton
2003-01-21  9:44   ` Dmitry A. Kazakov
2003-01-21 19:01     ` Martin Krischik
2003-01-21 11:43   ` Rodrigo García
2003-01-21 15:03   ` Stephen Leake
2003-01-21 13:09 ` Victor Porton
2003-01-22 15:26   ` Rodrigo García
2003-01-21 18:38 ` Victor Porton
2003-01-23 17:08   ` Stephen Leake

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