comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Incompatible types for array conversion
Date: Wed, 22 Jul 2015 20:50:25 +0300
Date: 2015-07-22T20:50:25+03:00	[thread overview]
Message-ID: <d1a3f1Fp5r4U1@mid.individual.net> (raw)
In-Reply-To: <d19jodFl411U1@mid.individual.net>

On 15-07-22 16:22 , hreba wrote:
> On 07/22/2015 10:00 AM, hreba wrote:
>> On 07/22/2015 02:20 AM, Niklas Holsti wrote:
>>> On 15-07-22 04:19 , hreba wrote:
>>>> I have a generic vector package:
>>>>
>>>> generic
>>>>     dim:        positive;
>>>>     type Real is digits <>;
>>>> package Vectors is
>>>>     type Vector is array (1..dim) of Real;
>>>>     function "*" (a: Vector; b: Real) return Vector;
>>>> end Vectors;
>>>>
>>>> In another package I want to model the definition of a (physical)
>>>> position vector by its direction and module:
>>>>
>>>> package Basic is
>>>>     type Real is new Interfaces.IEEE_Float_64;
>>>>     type Length is new Real;
>>>>
>>>>     package Positions is new Gen.Vectors (3, Length);
>>>>     package Directions is new Gen.Vectors (3, Real);
>>>>
>>>>     subtype Position is Positions.Vector;
>>>>     subtype Direction is Directions.Vector;
>>>>
>>>>     function "*" (l: Direction; r: Length) return Position;
>>>> end Basic;
>>>>
>>>> Now my attempt to define this function:
>>>>
>>>>     function "*" (l: Direction; r: Length) return Position is
>>>>     begin return Positions."*"(Position(l), r); end "*";
>>>>
>>>> gets me the error message in the subject line.
>>>>
>>>> Do I have to convert the vector element by element in a loop or is
>>>> there
>>>> a more elegant way?
>>>
>>> The conversion is rejected because of the conversion condition in RM 4.6
>>> (24.5/2): "The component subtypes shall statically match". The component
>>> (sub)types are Real and Length, which are different types, although
>>> Length is derived from Real. If you change the declaration of Length
>>> to be
>>>
>>>     subtype Length is Real;
>>>
>>> the conversion becomes legal.
>>>
>>
>> Thanks a lot for the hint!
>>
> Well, I was a little too fast with my response. It works, but it is not
> what I need. I use type Real for physical dimensionless quantities and
> Length for lengths. They must be incompatible, you cannot add them.

For physical units/dimensions, I believe the best current solution is 
the "aspects" approach in GNAT; see, for example:

https://gcc.gnu.org/onlinedocs/gnat_ugn/Performing-Dimensionality-Analysis-in-GNAT.html

http://www.christ-usch-grein.homepage.t-online.de/Ada/Dimension/Physical_units_with_GNAT_GPL_2013-AUJ35.1.pdf

It is still GNAT-specific, though.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

  reply	other threads:[~2015-07-22 17:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-22  1:19 Incompatible types for array conversion hreba
2015-07-22  5:20 ` Niklas Holsti
2015-07-22  7:13   ` Simon Wright
2015-07-22  7:41     ` Niklas Holsti
2015-07-22 12:32       ` Simon Wright
2015-07-27 23:12         ` Randy Brukardt
2015-07-22 13:00   ` hreba
2015-07-22 13:22     ` hreba
2015-07-22 17:50       ` Niklas Holsti [this message]
2015-07-22 19:02         ` Dmitry A. Kazakov
2015-07-22  6:49 ` Georg Bauhaus
2015-07-22 12:57   ` hreba
2015-07-22 14:22     ` G.B.
2015-07-23 13:21 ` hreba
replies disabled

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