Access to function returning class-wide type

This is a discussion on Access to function returning class-wide type within the ADA forums in Programming Languages category; Hello all. I'm new here, but I'll try to stick on. Let me start with a tricky problem: Consider following code: ---------------------------------------- package A is type Abstracted is abstract tagged null record; function Proc (N : Natural) return Abstracted is abstract; type The_Access_Type is access function (N : Natural) return Abstracted'Class; end A; -- package A body ommited; with A; package B is type Derived is new A.Abstracted with null record; function Proc (N : Natural) return Derived; Failing_Object : A.The_Access_Type := B.Proc'Access; -- will not compile, error: 'expected type The_Access_Type / found type access function Proc defined at ...' ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-21-2008, 06:13 AM
Paweł 'Nivertius' Płazieński
Guest
 
Default Access to function returning class-wide type

Hello all.

I'm new here, but I'll try to stick on.

Let me start with a tricky problem:

Consider following code:

----------------------------------------

package A is
type Abstracted is abstract tagged null record;
function Proc (N : Natural) return Abstracted is abstract;

type The_Access_Type is access function (N : Natural) return
Abstracted'Class;

end A;

-- package A body ommited;

with A;
package B is
type Derived is new A.Abstracted with null record;
function Proc (N : Natural) return Derived;

Failing_Object : A.The_Access_Type := B.Proc'Access;
-- will not compile, error: 'expected type The_Access_Type / found type
access function Proc defined at ...'

function Proc_Wrapper (N : Natural) return Abstracted'Class;

Working_Object : A.The_Access_Type := B.Proc_Wrapper'Access;
-- compiler doesn't complain

end B;

package B body is
function Proc_Wrapper (N : Natural) return Abstracted'Class is
begin
return Abstracted'Class'(Proc (N));
end Proc;

-- function Proc body ommited
end B;

----------------------------------------

What I need is a access type to 'constructor' of an derivate of abstract
object.
As I presented, there is a workaround, but that's not the 'right' way to do
it. I really want to do it without some tricky wrapping.
How do I define The_Access_Type to do what I want?

Thanks in advance.

--
Paweł Płazieński aka Nivertius
"In the end, there will be Ada, XML and gzip"
Reply With Quote
  #2  
Old 08-21-2008, 06:33 AM
Paweł 'Nivertius' Płazieński
Guest
 
Default Re: Access to function returning class-wide type

Little correction:

> function Proc_Wrapper (N : Natural) return Abstracted'Class is
> begin
> return Abstracted'Class'(Proc (N));
> end Proc;


Proc_Wrapper should return Derived'(Proc(N)); to workaround to work, but it
doesn't really change the problem.

--
Paweł Płazieński aka Nivertius
"In the end, there will be Ada, XML and gzip"
Reply With Quote
  #3  
Old 08-21-2008, 07:02 AM
Dmitry A. Kazakov
Guest
 
Default Re: Access to function returning class-wide type

On Thu, 21 Aug 2008 12:13:10 +0200, Pawe³ 'Nivertius' P³azieñski wrote:

> What I need is a access type to 'constructor' of an derivate of abstract
> object.
> As I presented, there is a workaround, but that's not the 'right' way to do
> it. I really want to do it without some tricky wrapping.
> How do I define The_Access_Type to do what I want?


See Ada.Tags.Generic_Dispatching_Constructor. It gives you a function
constructing an object from the type tag, the parameters (Natural in your
case) using the function you want (Proc). It goes as follows:

with Ada.Tags.Generic_Dispatching_Constructor;
package A is
type Abstracted is abstract tagged null record;
function Proc (N : not null access Natural)
return Abstracted is abstract;
function Constructor is
new Ada.Tags.Generic_Dispatching_Constructor
(Abstracted, Natural, Proc);
end A;

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Reply With Quote
  #4  
Old 08-21-2008, 07:56 AM
Paweł 'Nivertius' Płazieński
Guest
 
Default Re: Access to function returning class-wide type

Dmitry A. Kazakov wrote:

>> What I need is a access type to 'constructor' of an derivate of abstract
>> object.
>> As I presented, there is a workaround, but that's not the 'right' way to
>> do it. I really want to do it without some tricky wrapping.
>> How do I define The_Access_Type to do what I want?

> See Ada.Tags.Generic_Dispatching_Constructor. It gives you a function
> constructing an object from the type tag, the parameters (Natural in your
> case) using the function you want (Proc). It goes as follows:
>
> with Ada.Tags.Generic_Dispatching_Constructor;
> package A is
> type Abstracted is abstract tagged null record;
> function Proc (N : not null access Natural)
> return Abstracted is abstract;
> function Constructor is
> new Ada.Tags.Generic_Dispatching_Constructor
> (Abstracted, Natural, Proc);
> end A;


I belive that would work too, but at the cost of defining function Proc (N :
not null access Natural) return ... for each descendant type. That would be
another wrapper, so it's the same resolution to the problem as Proc_Wrapper
in my original code, assuming that I need Proc (N : Natural) as well.

Tell me that I'm wrong, please ;-)

--
Paweł Płazieński aka Nivertius
"In the end, there will be Ada, XML and gzip"
Reply With Quote
  #5  
Old 08-21-2008, 08:01 AM
Georg Bauhaus
Guest
 
Default Re: Access to function returning class-wide type

Paweł 'Nivertius' Płazieński schrieb:

> package B is
> type Derived is new A.Abstracted with null record;
> function Proc (N : Natural) return Derived;
>
> Failing_Object : A.The_Access_Type := B.Proc'Access;
> -- will not compile, error: 'expected type The_Access_Type / found type
> access function Proc defined at ...'


(Compiler is right because the profile of B.Proc is different
from that of The_Access_Type (whose functions return Abstracted'Class,
not Derived)

In general, public access types lead to unneccessary complication;
tagged types are by reference already, so you could simply use
dispatching and a factory instead. Do you have spefific needs
for pointers to functions returning by-reference objects?

Or follow Dmitry's advice.


package A is
type Abstracted is abstract tagged null record;
function Proc (N : Natural) return Abstracted is abstract;

package Constructor is
No_Suitable_Type: exception;

function Proc_Wrapper(N : Natural) return Abstracted'Class;
-- create objects of types in Abstracted'Class; the
-- specific type depends on N and other things that are
-- in scope in Constructor's body
end Constructor;

end A;

with A;
package B is
type Derived is new A.Abstracted with null record;
overriding
function Proc (N : Natural) return Derived;
end B;



package body A is
package body Constructor is

function Proc_Wrapper(N : Natural) return Abstracted'Class is
begin
if N > 666 then -- and possibly Some_Other_Condition ...
declare
use B;
begin
return Derived'(Proc(N));
end;
else
--
-- other cases TBD
--
raise No_Suitable_Type;
end if;
end Proc_Wrapper;

end Constructor;
end A;

package body B is
function Proc (N : Natural) return Derived is
begin
return Derived'(null record);
end Proc;
end B;


HTH
--
Georg Bauhaus
Y A Time Drain http://www.9toX.de
Reply With Quote
  #6  
Old 08-21-2008, 09:01 AM
Paweł 'Nivertius' Płazieński
Guest
 
Default Re: Access to function returning class-wide type

Georg Bauhaus wrote:

> Paweł 'Nivertius' Płazieński schrieb:
>
>> package B is
>> type Derived is new A.Abstracted with null record;
>> function Proc (N : Natural) return Derived;
>>
>> Failing_Object : A.The_Access_Type := B.Proc'Access;
>> -- will not compile, error: 'expected type The_Access_Type /
>> found type
>> access function Proc defined at ...'

> (Compiler is right because the profile of B.Proc is different
> from that of The_Access_Type (whose functions return Abstracted'Class,
> not Derived)


I made a test file:

package A is
type Abstracted is range 1 .. 100;
function Proc (N : Natural) return Abstracted;

type The_Access_Type is
access function (N : Natural) return Abstracted;
end A;

package body A is
function Proc (N : Natural) return Abstracted is
begin
return Abstracted (N);
end;
end A;

package B is
subtype Derived is A.Abstracted range 1 .. 10;
function Proc (N : Natural) return Derived;

Object : A.The_Access_Type := B.Proc'Access;
end B;

package body B is
function Proc (N : Natural) return Derived is
begin
return Derived (N);
end;
end B;

And this also fails to compile:
20. Object : A.The_Access_Type := B.Proc'Access;
|
>>> not subtype conformant with declaration at line 6
>>> return type does not match


So I assume that 'fully-conformant' means that types needs to be _exactly_
the same, not even implicitly conversible. That means that one wrapper or
another is required.

> In general, public access types lead to unneccessary complication;
> tagged types are by reference already, so you could simply use
> dispatching and a factory instead. Do you have spefific needs
> for pointers to functions returning by-reference objects?


Well, that was the example my problem boils down to. In real file the access
type is private within top-level package [in example it was A] and used
internally, not even in child packages.

> function Proc_Wrapper(N : Natural) return Abstracted'Class is
> begin
> if N > 666 then -- and possibly Some_Other_Condition ...
> declare
> use B;
> begin
> return Derived'(Proc(N));
> end;
> else
> --
> -- other cases TBD
> --
> raise No_Suitable_Type;
> end if;
> end Proc_Wrapper;


First of all, the problem with that is, that I couldn't define conditions
and types for them dynamicaly.
Second (of all?), each new Derived type in child, or even remotely other
package needs to be hard-coded into examples package A.

I have a map between condition on an object of some type (in example it
was 'N') and access-to-function which creates the object derived from
Abstracted. I've also got funcion which gets the 'object of some type' and
for each key in the map checks if the condition is true. If so,
calls 'constructor' passing that 'object of some type' and returns its
result. If no condition is true, the function raises an exception.
(I probably should wrote that in original post).

--
Paweł Płazieński aka Nivertius
"In the end, there will be Ada, XML and gzip"
Reply With Quote
  #7  
Old 08-21-2008, 09:34 AM
Dmitry A. Kazakov
Guest
 
Default Re: Access to function returning class-wide type

On Thu, 21 Aug 2008 13:56:35 +0200, Pawe³ 'Nivertius' P³azieñski wrote:

> Dmitry A. Kazakov wrote:
>
>>> What I need is a access type to 'constructor' of an derivate of abstract
>>> object.
>>> As I presented, there is a workaround, but that's not the 'right' way to
>>> do it. I really want to do it without some tricky wrapping.
>>> How do I define The_Access_Type to do what I want?

>> See Ada.Tags.Generic_Dispatching_Constructor. It gives you a function
>> constructing an object from the type tag, the parameters (Natural in your
>> case) using the function you want (Proc). It goes as follows:
>>
>> with Ada.Tags.Generic_Dispatching_Constructor;
>> package A is
>> type Abstracted is abstract tagged null record;
>> function Proc (N : not null access Natural)
>> return Abstracted is abstract;
>> function Constructor is
>> new Ada.Tags.Generic_Dispatching_Constructor
>> (Abstracted, Natural, Proc);
>> end A;

>
> I belive that would work too, but at the cost of defining function Proc (N :
> not null access Natural) return ... for each descendant type.


Yes, it is the body of the "constructor".

> That would be
> another wrapper, so it's the same resolution to the problem as Proc_Wrapper
> in my original code, assuming that I need Proc (N : Natural) as well.


Not really, because it would replace old Proc completely. The idea is:

with Ada.Tags; use Ada.Tags;
package A is
type Abstracted is abstract tagged null record;
-- To be used
function Factory (T : Tag; N : Natural) return Abstracted'Class;
-- Implementation detail, to be provided by each derived type
function Proc (N : not null access Natural)
return Abstracted is abstract;
end A;

with Ada.Tags.Generic_Dispatching_Constructor;
package body A is
function Make_It is
new Generic_Dispatching_Constructor (Abstracted, Natural, Proc);
function Factory (T : Tag; N : Natural) return Abstracted'Class is
Parameters : aliased Natural := N;
begin
return Make_It (T, Parameters'Access);
end Factory;
end A;

Now, when you derive from Abstracted you override Proc and that's all you
need to keep Factory working:

type Concrete is new Abstracted with private;
overriding Proc (N : not null access Natural return Concrete;

In short, the solution replaces pointer->constructor with tag->constructor.
Both enforced to be overridden.

P.S. I don't know reasons why it was decided to use an access type in
Ada.Tags.Generic_Dispatching_Constructor profile. One could always pass a
pointer there if a side effect on the parameters were desired. But that is
another issue, and in any case Proc is thought as an implementation detail,
so its exact profile should not really matter.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Reply With Quote
  #8  
Old 08-21-2008, 09:50 AM
Dmitry A. Kazakov
Guest
 
Default Re: Access to function returning class-wide type

On Thu, 21 Aug 2008 15:01:01 +0200, Pawe³ 'Nivertius' P³azieñski wrote:

> First of all, the problem with that is, that I couldn't define conditions
> and types for them dynamicaly.
> Second (of all?), each new Derived type in child, or even remotely other
> package needs to be hard-coded into examples package A.
>
> I have a map between condition on an object of some type (in example it
> was 'N') and access-to-function which creates the object derived from
> Abstracted. I've also got funcion which gets the 'object of some type' and
> for each key in the map checks if the condition is true. If so,
> calls 'constructor' passing that 'object of some type' and returns its
> result. If no condition is true, the function raises an exception.
> (I probably should wrote that in original post).


So you could replace it with a map condition->type tag and then proceed
with Ada.Tags.Generic_Dispatching_Constructor, because the map
tag->constructor is for free, here you do not need pointers any more, in
Ada 2005. The compiler will force to override the "constructor" for each
concrete derived type.

-------------
Having said that, there still is a difficult problem with construction of
the map. I don't know if it is an issue for you, but it is for the
persistency layer I designed. The problem is that if the map is open-ended,
you need some mechanism of registration of each new type in the map. This
is still unresolved in Ada. The pattern I am using is that each package
declaring a new type is required to call some class-wide procedure
registering the type upon package elaboration. In your case it is the
condition associated with the type tag. That cannot be enforced, and it
works only at the library level, i.e. for "immortal" types.

A possible solution in the future language revisions could be to introduce
type constructors/destructors: user-defined procedures called upon
derivation and leaving the scope of a type.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Reply With Quote
  #9  
Old 08-21-2008, 01:02 PM
Adam Beneschan
Guest
 
Default Re: Access to function returning class-wide type

On Aug 21, 3:13 am, Pawe³ 'Nivertius' P³azieñski <nivert...@gmail.com>
wrote:
> Hello all.
>
> I'm new here, but I'll try to stick on.
>
> Let me start with a tricky problem:
>
> Consider following code:
>
> ----------------------------------------
>
> package A is
> type Abstracted is abstract tagged null record;
> function Proc (N : Natural) return Abstracted is abstract;
>
> type The_Access_Type is access function (N : Natural) return
> Abstracted'Class;
>
> end A;
>
> -- package A body ommited;
>
> with A;
> package B is
> type Derived is new A.Abstracted with null record;
> function Proc (N : Natural) return Derived;
>
> Failing_Object : A.The_Access_Type := B.Proc'Access;
> -- will not compile, error: 'expected type The_Access_Type / found type
> access function Proc defined at ...'
>
> function Proc_Wrapper (N : Natural) return Abstracted'Class;
>
> Working_Object : A.The_Access_Type := B.Proc_Wrapper'Access;
> -- compiler doesn't complain
>
> end B;
>
> package B body is
> function Proc_Wrapper (N : Natural) return Abstracted'Class is
> begin
> return Abstracted'Class'(Proc (N));
> end Proc;
>
> -- function Proc body ommited
> end B;
>
> ----------------------------------------
>
> What I need is a access type to 'constructor' of an derivate of abstract
> object.
> As I presented, there is a workaround, but that's not the 'right' way to do
> it. I really want to do it without some tricky wrapping.
> How do I define The_Access_Type to do what I want?


Others have posted discussions of how to accomplish what you want, or
what we think you might want, using a different construct.

I just wanted to point out that, from a low-level standpoint, there's
no way to have an access type that could refer to *both* a function
that returns Abstracted'Class *and* a function that returns the
specific type Derived. The reason is that an access-to-function is
basically just going to hold the address of the function (and some
other information). A function that returns T'Class can't work the
same as a function that returns a specific type, because the function
that returns T'Class won't know in advance how big the return object
is going to be, so some dynamic heap (or "secondary stack") allocation
must be involved. A function that returns Derived, though, knows how
big the return object is, so the result can be pre-allocated. This
means that both the function and the function's caller have to adopt
different mechanisms for passing the return object between them. If
you have an access-to-function which could point to either one, the
code that makes a call through this access-to-function pointer isn't
going to know which return object mechanism would be used, so the
result would be a mess.

If Ada were to provide a mechanism so that B.Proc'Access could be
used, the compiler would have to generate its own wrapper, same as
you're doing. This isn't necessarily a bad thing; there are already
other cases where Ada compilers have to generate wrappers (at least
ours does). But I think it's useful to understand the underlying
mechanisms involved, to get a better understanding of why the language
won't let you do certain things.

By the way, if you decide that the other suggestions don't work and
you need to write a wrapper anyway, you shouldn't have to rewrite the
whole wrapper for every type; I think you can use a generic to make
the process of declaring the wrapper a lot simpler.

-- Adam

Reply With Quote
  #10  
Old 08-21-2008, 03:22 PM
Pawe³ 'Nivertius' P³azieñski
Guest
 
Default Re: Access to function returning class-wide type

Adam Beneschan wrote:

> Others have posted discussions of how to accomplish what you want, or
> what we think you might want, using a different construct.


I watched comp.lang.ada for little time, searched through posts which I got
and I belive I didn't see that. I'm sorry if I browsed to short.

> I just wanted to point out that, from a low-level standpoint, there's
> no way to have an access type that could refer to *both* a function
> that returns Abstracted'Class *and* a function that returns the
> specific type Derived. The reason is that an access-to-function is
> basically just going to hold the address of the function (and some
> other information). A function that returns T'Class can't work the
> same as a function that returns a specific type, because the function
> that returns T'Class won't know in advance how big the return object
> is going to be, so some dynamic heap (or "secondary stack") allocation
> must be involved. A function that returns Derived, though, knows how
> big the return object is, so the result can be pre-allocated. This
> means that both the function and the function's caller have to adopt
> different mechanisms for passing the return object between them. If
> you have an access-to-function which could point to either one, the
> code that makes a call through this access-to-function pointer isn't
> going to know which return object mechanism would be used, so the
> result would be a mess.
>
> If Ada were to provide a mechanism so that B.Proc'Access could be
> used, the compiler would have to generate its own wrapper, same as
> you're doing. This isn't necessarily a bad thing; there are already
> other cases where Ada compilers have to generate wrappers (at least
> ours does). But I think it's useful to understand the underlying
> mechanisms involved, to get a better understanding of why the language
> won't let you do certain things.


I think I understand what you want to show me. As I posted in other thread
[see news:g8jf3n$e00$1@registered.motzarella.org ], I've made a test with
type/subtype and it's failing as well. There is a more obvious collision
there, we expect a function which doesn't do dynamic range check on
returned object, and we pass a function which does.

> By the way, if you decide that the other suggestions don't work and
> you need to write a wrapper anyway, you shouldn't have to rewrite the
> whole wrapper for every type; I think you can use a generic to make
> the process of declaring the wrapper a lot simpler.


As I've shown in original post, the wrapper needs to consist only a return
statement from 'main' constructor. I'll try to do it with generic function
as you suggest, but in the worst case the difference will be... well...
small ;-)

--
Pawe³ P³azieñski aka Nivertius
"In the end, there will be Ada, XML and gzip"
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:27 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.