comp.lang.ada
 help / color / mirror / Atom feed
* Mystery with "others"
@ 2012-08-29 15:38 gautier_niouzes
  2012-08-29 16:44 ` Adam Beneschan
  0 siblings, 1 reply; 2+ messages in thread
From: gautier_niouzes @ 2012-08-29 15:38 UTC (permalink / raw)


Hello,

I get an error message with the initialization of variable s4 below, that I don't understand.
The workaround is easy, using the expression for variable s3.
Still, I am curious about the reason.

GNAT GPL 2012, with any of the modes Ada 95, 2005 and 2012, I get:
test_sets.adb:10:21: "others" choice not allowed here
ObjectAda 7.2.2 (Ada 95 only) is more verbose
test_sets.adb: Error: line 10 col 17 LRM:4.3.3(11), An OTHERS is not allowed in this context because there is no applicable_index_constraint, Continuing

So why is Ada unhappy with the expression after "s4 := " ?

---8<----8<----8<----8<--
procedure Test_sets is

  type Enum is (a,b,c,d);

  type Set is array(Enum) of Boolean;

  s1: Set := (a => True, others => False);
  s2: Set := (d => True, others => False);
  s3: Set := s1 or s2;
  s4: Set := s1 or (d => True, others => False); -- <- Line 10

begin
  null;
end;
---8<----8<----8<----8<--
Gautier's Ada programming 
http://gautiersblog.blogspot.com/search/label/Ada 
NB: follow the above link for a valid e-mail address 




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

* Re: Mystery with "others"
  2012-08-29 15:38 Mystery with "others" gautier_niouzes
@ 2012-08-29 16:44 ` Adam Beneschan
  0 siblings, 0 replies; 2+ messages in thread
From: Adam Beneschan @ 2012-08-29 16:44 UTC (permalink / raw)


On Wednesday, August 29, 2012 8:38:21 AM UTC-7, (unknown) wrote:
> Hello,
> 
> 
> 
> I get an error message with the initialization of variable s4 below, that I don't understand.
> 
> The workaround is easy, using the expression for variable s3.
> 
> Still, I am curious about the reason.
> 
> 
> 
> GNAT GPL 2012, with any of the modes Ada 95, 2005 and 2012, I get:
> 
> test_sets.adb:10:21: "others" choice not allowed here
> 
> ObjectAda 7.2.2 (Ada 95 only) is more verbose
> 
> test_sets.adb: Error: line 10 col 17 LRM:4.3.3(11), An OTHERS is not allowed in this context because there is no applicable_index_constraint, Continuing
> 
> 
> 
> So why is Ada unhappy with the expression after "s4 := " ?\

The "or" operator is a function, and the arguments to the function are declared as the *unconstrained* subtype of Set's base type.  Set is actually a constrained subtype of an array that looks like this:

   type [base-type] is array(Enum range <>) of Boolean;

and the function "or" is declared:

   function "or" (Left, Right : [base-type]) return [base-type];

See 4.5.1(3).  You have to understand the distinction between types and subtypes here.  Array *types* are unconstrained; subtypes of those types can be constrained or unconstrained.

When an aggregate like (d => True, others => False) shows up, the language needs to know how to get the bounds, or index constraints (that's what the phrase "applicable index constraint" in the error message means (see 4.3.3).  In the declarations of s1 and s2, there's a rule that if the aggregate is the expression on the right of :=, the variable on the left side will give the constraint.  So those declarations are OK.  But when the aggregate is the parameter to a function, and the function parameter doesn't have any constraints, there's nothing telling it what the constraints are.  (*We* know that for a predefined function like "or", the constraint of one parameter should be the same as the constraint of the other parameter because "or" needs its parameters to be the same size.  But that isn't true for all functions.  So there can't really be a general rule to handle that case.)  Since there's nothing telling it what the bounds are, the compiler has to reject the aggregate.

This will work:

   s4: Set := s1 or Set'(d => True, others => False);

Now the Set' qualifier tells the compiler what the bounds are going to be.

                           -- Adam


> 
> procedure Test_sets is
> 
>   type Enum is (a,b,c,d);
> 
>   type Set is array(Enum) of Boolean;
> 
>   s1: Set := (a => True, others => False);
>   s2: Set := (d => True, others => False);
>   s3: Set := s1 or s2;
>   s4: Set := s1 or (d => True, others => False); -- <- Line 10



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

end of thread, other threads:[~2012-08-29 16:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-29 15:38 Mystery with "others" gautier_niouzes
2012-08-29 16:44 ` Adam Beneschan

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