comp.lang.ada
 help / color / mirror / Atom feed
* A good program that not compile
@ 2014-05-21 13:05 Victor Porton
  2014-05-21 13:49 ` Simon Wright
  0 siblings, 1 reply; 5+ messages in thread
From: Victor Porton @ 2014-05-21 13:05 UTC (permalink / raw)


The following does not compile.

Nevertheless it is a logical code and it or it variant should be compilable
in a good programming language. I humbly ask: may we make it compilable in
the next version of Ada?

-- my.ads
with Ada.Finalization;

package My is

   type Abstract_Handle is new Ada.Finalization.Limited_Controlled with
      record
         Handle: Integer;
      end record;

   function Obtain_Handle(Object: Abstract_Handle) return Integer is abstract;

   overriding procedure Initialize(Object: in out Abstract_Handle);

   type File_Handle is new Abstract_Handle with null record;

   overriding procedure Obtain_Handle(Object: in out File_Handle);

end My;

-- my.adb
package body My is

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Object);
   end;

   overriding function Obtain_Handle(Object: in out File_Handle) return Integer is
   begin
      return 123; -- Suppose 123 is a file handle, for an example
   end;

end My;


-- 
Victor Porton - http://portonvictor.org

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

* Re: A good program that not compile
  2014-05-21 13:05 A good program that not compile Victor Porton
@ 2014-05-21 13:49 ` Simon Wright
  2014-05-21 14:10   ` Victor Porton
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Wright @ 2014-05-21 13:49 UTC (permalink / raw)


Victor Porton <porton@narod.ru> writes:

> The following does not compile.

For very good reasons.

Obtain_Handle(File_Handle) ought to be a function!
It should take an in parameter, not in out!

I take it your point is that you want to be able to declare 

   function Obtain_Handle(Object: Abstract_Handle) return Integer
   is abstract;

but I find that (with the latest compilers) this works:

   function Obtain_Handle(Object: Abstract_Handle) return Integer
   is (raise Program_Error);

with the slight disadvantage of postponing failure to run time (which
might happen anyway with your proposal).



with Ada.Finalization;

package My is

   type Abstract_Handle is new Ada.Finalization.Limited_Controlled with
      record
         Handle: Integer;
      end record;

   function Obtain_Handle(Object: Abstract_Handle) return Integer
   is (raise Program_Error);

   overriding procedure Initialize(Object: in out Abstract_Handle);

   type File_Handle is new Abstract_Handle with null record;

   overriding function Obtain_Handle(Object: File_Handle) return Integer;

end My;

package body My is

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Abstract_Handle'Class (Object));
   end;

   overriding function Obtain_Handle(Object: File_Handle) return Integer is
   begin
      return 123; -- Suppose 123 is a file handle, for an example
   end;

end My;


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

* Re: A good program that not compile
  2014-05-21 13:49 ` Simon Wright
@ 2014-05-21 14:10   ` Victor Porton
  2014-05-21 14:24     ` Victor Porton
  2014-05-21 15:58     ` Simon Wright
  0 siblings, 2 replies; 5+ messages in thread
From: Victor Porton @ 2014-05-21 14:10 UTC (permalink / raw)


Simon Wright wrote:

> Victor Porton <porton@narod.ru> writes:
> 
>> The following does not compile.
> 
> For very good reasons.
> 
> Obtain_Handle(File_Handle) ought to be a function!
> It should take an in parameter, not in out!
> 
> I take it your point is that you want to be able to declare
> 
>    function Obtain_Handle(Object: Abstract_Handle) return Integer
>    is abstract;
> 
> but I find that (with the latest compilers) this works:
> 
>    function Obtain_Handle(Object: Abstract_Handle) return Integer
>    is (raise Program_Error);
> 
> with the slight disadvantage of postponing failure to run time (which
> might happen anyway with your proposal).

I have rewritten it with your suggestions. Now it does compile, but instead
as of to work as intended (Assigning 123 to the Handle) it raises
Program_Error.

Can we save my program and make it work as intended?

The listing follows:

-- my.ads
with Ada.Finalization;

package My is

   type Abstract_Handle is new Ada.Finalization.Limited_Controlled with
      record
         Handle: Integer;
      end record;

   function Obtain_Handle(Object: Abstract_Handle) return Integer;

   overriding procedure Initialize(Object: in out Abstract_Handle);

   type File_Handle is new Abstract_Handle with null record;

   overriding function Obtain_Handle(Object: File_Handle) return Integer;

end My;


-- my.adb
package body My is

   function Obtain_Handle(Object: Abstract_Handle) return Integer is
   begin
      raise Program_Error;
      return 0;
   end;

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Object);
   end;

   overriding function Obtain_Handle(Object: File_Handle) return Integer is
   begin
      return 123; -- Suppose 123 is a file handle, for an example
   end;

end My;

-- main.adb
with My;
with Ada.Text_IO;

procedure Main is
   X: My.File_Handle;
begin
   Ada.Text_IO.Put_Line(Integer'Image(X.Handle));
end;

-- 
Victor Porton - http://portonvictor.org


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

* Re: A good program that not compile
  2014-05-21 14:10   ` Victor Porton
@ 2014-05-21 14:24     ` Victor Porton
  2014-05-21 15:58     ` Simon Wright
  1 sibling, 0 replies; 5+ messages in thread
From: Victor Porton @ 2014-05-21 14:24 UTC (permalink / raw)


Now it works! 100%

No need to change Ada standard.

The latest listing follows:

-- my.ads
with Ada.Finalization;

package My is

   type Abstract_Handle is abstract new Ada.Finalization.Limited_Controlled with
      record
         Handle: Integer;
      end record;

   function Obtain_Handle(Object: Abstract_Handle) return Integer is abstract;

   overriding procedure Initialize(Object: in out Abstract_Handle);

   type File_Handle is new Abstract_Handle with null record;

   overriding function Obtain_Handle(Object: File_Handle) return Integer;

end My;

-- my.adb
package body My is

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Abstract_Handle'Class(Object));
   end;

   overriding function Obtain_Handle(Object: File_Handle) return Integer is
   begin
      return 123; -- Suppose 123 is a file handle, for an example
   end;

end My;

-- main.adb
with My;
with Ada.Text_IO;

procedure Main is
   X: My.File_Handle;
begin
   Ada.Text_IO.Put_Line(Integer'Image(X.Handle));
end;


-- 
Victor Porton - http://portonvictor.org


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

* Re: A good program that not compile
  2014-05-21 14:10   ` Victor Porton
  2014-05-21 14:24     ` Victor Porton
@ 2014-05-21 15:58     ` Simon Wright
  1 sibling, 0 replies; 5+ messages in thread
From: Simon Wright @ 2014-05-21 15:58 UTC (permalink / raw)


Victor Porton <porton@narod.ru> writes:

> Simon Wright wrote:
>
>> Victor Porton <porton@narod.ru> writes:
>> 
>>> The following does not compile.
>> 
>> For very good reasons.
>> 
>> Obtain_Handle(File_Handle) ought to be a function!
>> It should take an in parameter, not in out!
>> 
>> I take it your point is that you want to be able to declare
>> 
>>    function Obtain_Handle(Object: Abstract_Handle) return Integer
>>    is abstract;
>> 
>> but I find that (with the latest compilers) this works:
>> 
>>    function Obtain_Handle(Object: Abstract_Handle) return Integer
>>    is (raise Program_Error);
>> 
>> with the slight disadvantage of postponing failure to run time (which
>> might happen anyway with your proposal).
>
> I have rewritten it with your suggestions. Now it does compile, but instead
> as of to work as intended (Assigning 123 to the Handle) it raises
> Program_Error.

You have written (as before)

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Object);
   end;

but I wrote

   overriding procedure Initialize(Object: in out Abstract_Handle) is
   begin
      Object.Handle := Obtain_Handle(Abstract_Handle'Class (Object));
      --                             ^^^^^^^^^^^^^^^^^^^^^^^      ^
      --                        use view conversion to get dispatching
   end;

In your case, the call to Obtain_Handle is *not* dispatching. You need a
classwide object (or access) to get dispatching; as you see, you can get
this by a view conversion.


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

end of thread, other threads:[~2014-05-21 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-21 13:05 A good program that not compile Victor Porton
2014-05-21 13:49 ` Simon Wright
2014-05-21 14:10   ` Victor Porton
2014-05-21 14:24     ` Victor Porton
2014-05-21 15:58     ` Simon Wright

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