comp.lang.ada
 help / color / mirror / Atom feed
* Vector (container) initialization: the schizophrenic ampersand
@ 2012-05-17 20:20 Marius Amado-Alves
  2012-05-17 20:24 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Marius Amado-Alves @ 2012-05-17 20:20 UTC (permalink / raw)


Shouldn't this be the right idiom to initialize a vector (container) with literals:

use My_Vectors;
V : My_Vectors.Vector := Element1 & Element2 & Element3;

Looks nice, but does not compile (with GNAT), because the compiler cannot make a decision on which "&" function to use where. Is there a way to give precedence to one of the "&" functions? Or some other nice solution? Thanks.

/*
My current solution is to rename one of the functions:

use My_Vectors;

function "+" (L : My_Vectors.Vector; R : My_Element_Type)
   renames My_Vectors."&";

V : My_Vectors.Vector := Element1 & Element2 + Element3;

Not elegant at all.
*/



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-17 20:20 Vector (container) initialization: the schizophrenic ampersand Marius Amado-Alves
@ 2012-05-17 20:24 ` Jeffrey Carter
  2012-05-17 20:26 ` Pascal Obry
  2012-05-17 23:33 ` Adam Beneschan
  2 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2012-05-17 20:24 UTC (permalink / raw)


On 05/17/2012 01:20 PM, Marius Amado-Alves wrote:
> Shouldn't this be the right idiom to initialize a vector (container) with
> literals:
>
> use My_Vectors; V : My_Vectors.Vector := Element1&  Element2&  Element3;
>
> Looks nice, but does not compile (with GNAT), because the compiler cannot
> make a decision on which "&" function to use where. Is there a way to give
> precedence to one of the"&" functions? Or some other nice solution? Thanks.

Does using parentheses help?

V : Vector := (E1 & E2) & E3;

-- 
Jeff Carter
Just as Khan was hindered by two-dimensional thinking in a
three-dimensional situation, so many developers are hindered
by sequential thinking in concurrent situations.
118

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-17 20:20 Vector (container) initialization: the schizophrenic ampersand Marius Amado-Alves
  2012-05-17 20:24 ` Jeffrey Carter
@ 2012-05-17 20:26 ` Pascal Obry
  2012-05-17 23:33 ` Adam Beneschan
  2 siblings, 0 replies; 16+ messages in thread
From: Pascal Obry @ 2012-05-17 20:26 UTC (permalink / raw)


Le 17/05/2012 22:20, Marius Amado-Alves a �crit :
> Shouldn't this be the right idiom to initialize a vector (container) with literals:
> 
> use My_Vectors;
> V : My_Vectors.Vector := Element1 & Element2 & Element3;

Won't this works:

  V : My_Vectors.Vector := (Element1 & Element2) & Element3;

?

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-17 20:20 Vector (container) initialization: the schizophrenic ampersand Marius Amado-Alves
  2012-05-17 20:24 ` Jeffrey Carter
  2012-05-17 20:26 ` Pascal Obry
@ 2012-05-17 23:33 ` Adam Beneschan
  2012-05-18  6:34   ` Marius Amado-Alves
  2012-05-18  6:51   ` Marius Amado-Alves
  2 siblings, 2 replies; 16+ messages in thread
From: Adam Beneschan @ 2012-05-17 23:33 UTC (permalink / raw)


On Thursday, May 17, 2012 1:20:31 PM UTC-7, marius63 wrote:
> Shouldn't this be the right idiom to initialize a vector (container) with literals:
> 
> use My_Vectors;
> V : My_Vectors.Vector := Element1 & Element2 & Element3;
> 
> Looks nice, but does not compile (with GNAT), because the compiler cannot make a decision on which "&" function to use where. Is there a way to give precedence to one of the "&" functions? Or some other nice solution? Thanks.
> 
> /*
> My current solution is to rename one of the functions:
> 
> use My_Vectors;
> 
> function "+" (L : My_Vectors.Vector; R : My_Element_Type)
>    renames My_Vectors."&";
> 
> V : My_Vectors.Vector := Element1 & Element2 + Element3;
> 
> Not elegant at all.
> */

It compiled for me the first time I tried it, so I think we need more information, in particular what your element type is.  A complete example that compiles except for that error would be helpful.

Parentheses won't help.  The operator precedence rules mean that the expression is always interpreted as (Element1 & Element2) & Element3 regardless of what functions the operators refer to.  

If your element type is some sort of array, then there could be a conflict with the built-in array concatenation operator.  The solution might be

   V : My_Vectors.Vector := My_Vectors.Vector'(Element1 & Element2) & Element3;

to make it clear that the first "&" must be the one that returns a Vector.

                           -- Adam



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-17 23:33 ` Adam Beneschan
@ 2012-05-18  6:34   ` Marius Amado-Alves
  2012-05-18  6:51   ` Marius Amado-Alves
  1 sibling, 0 replies; 16+ messages in thread
From: Marius Amado-Alves @ 2012-05-18  6:34 UTC (permalink / raw)


> If your element type is some sort of array, then there could be a conflict with the built-in array concatenation operator.  (Adam)

The element are indeed an array, but the compiler only mentions the vector "&" functions in the error messages... But you can still be right. I'll check.

Thanks a lot.



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-17 23:33 ` Adam Beneschan
  2012-05-18  6:34   ` Marius Amado-Alves
@ 2012-05-18  6:51   ` Marius Amado-Alves
  2012-05-18 13:31     ` Robert A Duff
  2012-05-19  9:28     ` Dmitry A. Kazakov
  1 sibling, 2 replies; 16+ messages in thread
From: Marius Amado-Alves @ 2012-05-18  6:51 UTC (permalink / raw)


Adam is right: it is confusion with the predefined "&" for arrays, when the elements are arrays. (But the compiler error messages indicate undecision only between the two Vectors "&" functions.) So my new solution is

function "+" (L : My_Vectors.Vector; R : My_Element_Type) 
   renames My_Vectors."&"; 

function "+" (L, R : My_Element_Type) 
   renames My_Vectors."&"; 

V : My_Vectors.Vector := Element1 + Element2 + Element3;

A bit less inelegant.

/* But more than parenthesis or T'(...) in my view. */

Thanks a lot. This group is outstanding.



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-18  6:51   ` Marius Amado-Alves
@ 2012-05-18 13:31     ` Robert A Duff
  2012-05-18 18:55       ` Shark8
  2012-05-19  9:03       ` Marius Amado-Alves
  2012-05-19  9:28     ` Dmitry A. Kazakov
  1 sibling, 2 replies; 16+ messages in thread
From: Robert A Duff @ 2012-05-18 13:31 UTC (permalink / raw)


Marius Amado-Alves <amado.alves@gmail.com> writes:

> Adam is right: it is confusion with the predefined "&" for arrays, when the elements are arrays. (But the compiler error messages indicate undecision only between the two Vectors "&" functions.) So my new solution is
>
> function "+" (L : My_Vectors.Vector; R : My_Element_Type) 
>    renames My_Vectors."&"; 
>
> function "+" (L, R : My_Element_Type) 
>    renames My_Vectors."&"; 
>
> V : My_Vectors.Vector := Element1 + Element2 + Element3;
>
> A bit less inelegant.
>
> /* But more than parenthesis or T'(...) in my view. */

You mean more inelegant, or more elegant?

IMHO, a qualified expression is the elegant (and usual)
way to resolve ambiguities.  Renaming "&" to be "+" just
seems confusing, to me.

- Bob



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-18 13:31     ` Robert A Duff
@ 2012-05-18 18:55       ` Shark8
  2012-05-18 22:44         ` Robert A Duff
  2012-05-19  9:03       ` Marius Amado-Alves
  1 sibling, 1 reply; 16+ messages in thread
From: Shark8 @ 2012-05-18 18:55 UTC (permalink / raw)


On Friday, May 18, 2012 8:31:59 AM UTC-5, Robert A Duff wrote:
>
> IMHO, a qualified expression is the elegant (and usual)
> way to resolve ambiguities.  Renaming "&" to be "+" just
> seems confusing, to me.
> 
> - Bob

Isn't that the whole point of having the qualified expression, to tell the compiler "this is the type for this ambiguous operation"?



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-18 18:55       ` Shark8
@ 2012-05-18 22:44         ` Robert A Duff
  0 siblings, 0 replies; 16+ messages in thread
From: Robert A Duff @ 2012-05-18 22:44 UTC (permalink / raw)


Shark8 <onewingedshark@gmail.com> writes:

> On Friday, May 18, 2012 8:31:59 AM UTC-5, Robert A Duff wrote:
>>
>> IMHO, a qualified expression is the elegant (and usual)
>> way to resolve ambiguities.  Renaming "&" to be "+" just
>> seems confusing, to me.
>> 
>> - Bob
>
> Isn't that the whole point of having the qualified expression, to tell
> the compiler "this is the type for this ambiguous operation"?

Yes, exactly.

Although qualified expressions also do some run-time checks, so
it's not the whole point, but the main point.

- Bob



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-18 13:31     ` Robert A Duff
  2012-05-18 18:55       ` Shark8
@ 2012-05-19  9:03       ` Marius Amado-Alves
  2012-05-19 16:15         ` Robert A Duff
  2012-05-21 15:57         ` Adam Beneschan
  1 sibling, 2 replies; 16+ messages in thread
From: Marius Amado-Alves @ 2012-05-19  9:03 UTC (permalink / raw)


> > V : My_Vectors.Vector := Element1 + Element2 + Element3;
> >
> > A bit less inelegant.
> >
> > /* But more than parenthesis or T'(...) in my view. */
> 
> You mean more inelegant, or more elegant?

Oops, sorry, I mean more elegant, i.e. I find

   Element1 + Element2 + Element3

more elegant than

   (T'(Element1) & T'(Element2)) & T'(Element3)

or variations thereof. (Textual elegancy. I agree the latter form has more semantic rigour = abstract elegancy.)



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-18  6:51   ` Marius Amado-Alves
  2012-05-18 13:31     ` Robert A Duff
@ 2012-05-19  9:28     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 16+ messages in thread
From: Dmitry A. Kazakov @ 2012-05-19  9:28 UTC (permalink / raw)


On Thu, 17 May 2012 23:51:52 -0700 (PDT), Marius Amado-Alves wrote:

> Adam is right: it is confusion with the predefined "&" for arrays, when
> the elements are arrays. (But the compiler error messages indicate
> undecision only between the two Vectors "&" functions.) So my new solution
> is
> 
> function "+" (L : My_Vectors.Vector; R : My_Element_Type) 
>    renames My_Vectors."&"; 
> 
> function "+" (L, R : My_Element_Type) 
>    renames My_Vectors."&"; 
> 
> V : My_Vectors.Vector := Element1 + Element2 + Element3;

What if My_Element_Type is numeric?

It is always problematic to mix sets and elements of. The idea to overload
an operation for both is attractive but guaranteed to have issues.

Semantically correct is: 

   (Element1, Element2, Element3)

Yet another trick to disambiguate:

   Empty & Element1 & Element2 & Element3

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-19  9:03       ` Marius Amado-Alves
@ 2012-05-19 16:15         ` Robert A Duff
  2012-05-21 16:02           ` Adam Beneschan
  2012-05-21 15:57         ` Adam Beneschan
  1 sibling, 1 reply; 16+ messages in thread
From: Robert A Duff @ 2012-05-19 16:15 UTC (permalink / raw)


Marius Amado-Alves <amado.alves@gmail.com> writes:

>> > V : My_Vectors.Vector := Element1 + Element2 + Element3;
>> >
>> > A bit less inelegant.
>> >
>> > /* But more than parenthesis or T'(...) in my view. */
>> 
>> You mean more inelegant, or more elegant?
>
> Oops, sorry, I mean more elegant, i.e. I find
>
>    Element1 + Element2 + Element3
>
> more elegant than
>
>    (T'(Element1) & T'(Element2)) & T'(Element3)
>
> or variations thereof. (Textual elegancy. I agree the latter form has
> more semantic rigour = abstract elegancy.)

You don't need to qualify everything.  Just enough to disambiguate.
I don't know what operators are visible in this example,
nor what the context is, but this should work:

    T'(Element1 & Element2 & Element3)

- Bob



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-19  9:03       ` Marius Amado-Alves
  2012-05-19 16:15         ` Robert A Duff
@ 2012-05-21 15:57         ` Adam Beneschan
  1 sibling, 0 replies; 16+ messages in thread
From: Adam Beneschan @ 2012-05-21 15:57 UTC (permalink / raw)


On Saturday, May 19, 2012 2:03:56 AM UTC-7, marius63 wrote:
> > > V : My_Vectors.Vector := Element1 + Element2 + Element3;
> > >
> > > A bit less inelegant.
> > >
> > > /* But more than parenthesis or T'(...) in my view. */
> > 
> > You mean more inelegant, or more elegant?
> 
> Oops, sorry, I mean more elegant, i.e. I find
> 
>    Element1 + Element2 + Element3
> 
> more elegant than
> 
>    (T'(Element1) & T'(Element2)) & T'(Element3)
> 
> or variations thereof. (Textual elegancy. I agree the latter form has more semantic rigour = abstract elegancy.)

FYI, the last expression won't work the way you wrote it.  If Element1 is an object declared with type T, then saying T'(Element1) won't gain you anything (other than possibly clarifying things for a reader), since the compiler already knows that Element1 has type T.  The & operation needs to be enclosed in the parentheses in T'(...) to make a difference.  That's what tells the compiler that when there are competing possibilities for &, to use the one that returns T.

As for which one is more elegant: this is a matter of opinion, but personally, if you have a small (maybe 15 lines) subprogram or block that uses this expression, and the "+" rename is declared locally within that subprogram or block, it wouldn't be hard to figure out.  But if I'm trying to read your code, and I see this expression and start having to hunt for what "+" means, the result will probably include a set of curse words that are not very elegant.  So in that sense, using the "+" rename reduces overall elegance.  I'm sure you already realize this, but for others' benefit: elegance is a worthwhile goal only if it enhances readability by removing clutter without doing anything else that makes it harder to understand.  Otherwise, the only way elegance is useful is if your goal is to hang a framed listing of your program on the wall, rather than to run it.

                           -- Adam 



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-19 16:15         ` Robert A Duff
@ 2012-05-21 16:02           ` Adam Beneschan
  2012-05-21 17:04             ` Robert A Duff
  0 siblings, 1 reply; 16+ messages in thread
From: Adam Beneschan @ 2012-05-21 16:02 UTC (permalink / raw)


On Saturday, May 19, 2012 9:15:44 AM UTC-7, Robert A Duff wrote:

> You don't need to qualify everything.  Just enough to disambiguate.
> I don't know what operators are visible in this example,
> nor what the context is, but this should work:
> 
>     T'(Element1 & Element2 & Element3)

Sorry, it won't work.  Assume all the elements have some array type Arr.  Syntactically, the above is equivalent to

     Vector'((Element1 & Element2) & Element3)

before any overload resolution is done.  The compiler still cannot tell whether the above means

     Vector'(Arr'(Element1 & Element2) & Element3)
or
     Vector'(Vector'(Element1 & Element2) & Element3)

So it's still ambiguous.

                           -- Adam



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-21 16:02           ` Adam Beneschan
@ 2012-05-21 17:04             ` Robert A Duff
  2012-05-21 17:57               ` Adam Beneschan
  0 siblings, 1 reply; 16+ messages in thread
From: Robert A Duff @ 2012-05-21 17:04 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Saturday, May 19, 2012 9:15:44 AM UTC-7, Robert A Duff wrote:
>
>> You don't need to qualify everything.  Just enough to disambiguate.
>> I don't know what operators are visible in this example,
>> nor what the context is, but this should work:
>> 
>>     T'(Element1 & Element2 & Element3)
>
> Sorry, it won't work.

Hmm.  You're probably right.  I'm not sure, because I haven't
seen what "&" operators we're talking about, nor what
the context is (is the concatenation assigned into
a variable, passed to an overloaded procedure, ...?).

- Bob



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

* Re: Vector (container) initialization: the schizophrenic ampersand
  2012-05-21 17:04             ` Robert A Duff
@ 2012-05-21 17:57               ` Adam Beneschan
  0 siblings, 0 replies; 16+ messages in thread
From: Adam Beneschan @ 2012-05-21 17:57 UTC (permalink / raw)


On Monday, May 21, 2012 10:04:30 AM UTC-7, Robert A Duff wrote:
> Adam Beneschan writes:
> 
> > On Saturday, May 19, 2012 9:15:44 AM UTC-7, Robert A Duff wrote:
> >
> >> You don't need to qualify everything.  Just enough to disambiguate.
> >> I don't know what operators are visible in this example,
> >> nor what the context is, but this should work:
> >> 
> >>     T'(Element1 & Element2 & Element3)
> >
> > Sorry, it won't work.
> 
> Hmm.  You're probably right.  I'm not sure, because I haven't
> seen what "&" operators we're talking about, nor what
> the context is (is the concatenation assigned into
> a variable, passed to an overloaded procedure, ...?).

"T" is the Vector type from an instance of Ada.Containers.Vectors.  The & operators are also from the instance and have been made directly visible with "use".  The & operators can take all four combinations of Element&Element, Element&Vector, Vector&Element, Vector&Vector.  

                        -- Adam




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

end of thread, other threads:[~2012-05-21 18:00 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-17 20:20 Vector (container) initialization: the schizophrenic ampersand Marius Amado-Alves
2012-05-17 20:24 ` Jeffrey Carter
2012-05-17 20:26 ` Pascal Obry
2012-05-17 23:33 ` Adam Beneschan
2012-05-18  6:34   ` Marius Amado-Alves
2012-05-18  6:51   ` Marius Amado-Alves
2012-05-18 13:31     ` Robert A Duff
2012-05-18 18:55       ` Shark8
2012-05-18 22:44         ` Robert A Duff
2012-05-19  9:03       ` Marius Amado-Alves
2012-05-19 16:15         ` Robert A Duff
2012-05-21 16:02           ` Adam Beneschan
2012-05-21 17:04             ` Robert A Duff
2012-05-21 17:57               ` Adam Beneschan
2012-05-21 15:57         ` Adam Beneschan
2012-05-19  9:28     ` Dmitry A. Kazakov

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