comp.lang.ada
 help / color / mirror / Atom feed
* Dynamic_Predicate and dispatching call
@ 2016-11-07 15:00 Duckysmokton
  2016-11-07 15:34 ` Egil H H
  0 siblings, 1 reply; 3+ messages in thread
From: Duckysmokton @ 2016-11-07 15:00 UTC (permalink / raw)


Hello,

I have a confusing issue with an Ada 2012 / GNAT 2016 program.

Given a tagged type that has :
* an abstract method that makes an unspecified calculation
* a non-abstract method that performs a dispatching call to this calculation
* a Dynamic_Predicate

package Test_Dispatch is

   type t_Element_Array is array (Natural range <>) of Integer;

   type t_Base_Object is abstract tagged private;

   function get_One_Value (Self    : in t_Base_Object;
                           Element : in Natural)
                           return Integer is abstract;

   function get_All_Values (Self : in t_Base_Object)
                            return t_Element_Array
     with Extensions_Visible; -- needed for dispatching call

private

   type t_Base_Object is abstract tagged record
      Element_Count : Positive;
   end record
     with Dynamic_Predicate => (t_Base_Object.Element_Count mod 2 = 0);

end Test_Dispatch;

package body Test_Dispatch is

   function get_All_Values (Self : in t_Base_Object)
                            return t_Element_Array is
   begin
      return Answer : t_Element_Array (0..Self.Element_Count-1) do
         for Element in Answer'Range loop
            Answer (Element) :=
            t_Base_Object'Class(Self).get_One_Value (Element);
         end loop;
      end return;
   end get_All_Values;

end Test_Dispatch;

Compilation fails on "t_Base_Object'Class(Self)." with :
class-wide argument not allowed here

If I remove the Dynamic_Predicate, it's OK.
ARM does not mention restriction on dispatching calls and dynamic predicates, so, what is the problem ?
Thanks for any advice.

PS:
- It also occurs if get_One_Value is not abstract (returns a defaultvalue)
- get_All_Values parameter Self shall not be class-wide because it is a default method and derived objects might implement more efficient or appropriate algorithms

(Troll bait : this would not happen with C++ !)

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

* Re: Dynamic_Predicate and dispatching call
  2016-11-07 15:00 Dynamic_Predicate and dispatching call Duckysmokton
@ 2016-11-07 15:34 ` Egil H H
  2016-11-15 13:12   ` Duckysmokton
  0 siblings, 1 reply; 3+ messages in thread
From: Egil H H @ 2016-11-07 15:34 UTC (permalink / raw)



Wild guess: Remove the Gnat (SPARK) specific aspect Extensions_Visible...

From the description[1] of Extensions_Visible:
"For example, if a formal parameter of a specific tagged type T is converted to a class-wide type and then used as a controlling operand in a dispatching call, then the (dynamic) callee might reference components of the parameter which are declared in some extension of T."

(As for the troll bait: wrong usage of compiler specific stuff could definitely cause similar behaviour in C++, or any other language.)

[1]. http://docs.adacore.com/spark2014-docs/html/lrm/subprograms.html#extensions-visible-aspects

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

* Re: Dynamic_Predicate and dispatching call
  2016-11-07 15:34 ` Egil H H
@ 2016-11-15 13:12   ` Duckysmokton
  0 siblings, 0 replies; 3+ messages in thread
From: Duckysmokton @ 2016-11-15 13:12 UTC (permalink / raw)


Le lundi 7 novembre 2016 16:34:23 UTC+1, Egil H H a écrit :
> Wild guess: Remove the Gnat (SPARK) specific aspect Extensions_Visible...
> 
> From the description[1] of Extensions_Visible:
> "For example, if a formal parameter of a specific tagged type T is converted to a class-wide type and then used as a controlling operand in a dispatching call, then the (dynamic) callee might reference components of the parameter which are declared in some extension of T."
> 
> (As for the troll bait: wrong usage of compiler specific stuff could definitely cause similar behaviour in C++, or any other language.)
> 
> [1]. http://docs.adacore.com/spark2014-docs/html/lrm/subprograms.html#extensions-visible-aspects

Interesting guess, but no... removing the aspect does not change the behavior of the compiler
(indeed, this aspect is required only if SPARK_Mode is on).

Thanx anyway

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

end of thread, other threads:[~2016-11-15 13:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-07 15:00 Dynamic_Predicate and dispatching call Duckysmokton
2016-11-07 15:34 ` Egil H H
2016-11-15 13:12   ` Duckysmokton

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