comp.lang.ada
 help / color / mirror / Atom feed
* Limited object by reference or access argument?
@ 2017-08-06 21:16 Victor Porton
  2017-08-06 22:01 ` Victor Porton
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Victor Porton @ 2017-08-06 21:16 UTC (permalink / raw)


Suppose we have

type World_Type is tagged limited private;

type BNode_ID_Handler is abstract tagged limited private;

We need to set a bnode handler for a world object. Should the procedure 
signature be

procedure Set_Handler (World: World_Type;
                       Handler: access BNode_ID_Handler'Class);

or 

procedure Set_Handler (World: World_Type;
                       Handler: BNode_ID_Handler'Class);

The later can work with limited BNode_ID_Handler type, because tagged and 
limited types are always passed by reference.

I prefer the second one, because with the first procedure signature it is 
possible to pass an access to an already finalized object what is an error, 
but want to ask you opinion which of the two procedure signatures is better.

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

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

* Re: Limited object by reference or access argument?
  2017-08-06 21:16 Limited object by reference or access argument? Victor Porton
@ 2017-08-06 22:01 ` Victor Porton
  2017-08-06 23:21   ` Victor Porton
  2017-08-07 14:10 ` Jere
  2017-08-07 23:28 ` Randy Brukardt
  2 siblings, 1 reply; 7+ messages in thread
From: Victor Porton @ 2017-08-06 22:01 UTC (permalink / raw)


Victor Porton wrote:

> Suppose we have
> 
> type World_Type is tagged limited private;
> 
> type BNode_ID_Handler is abstract tagged limited private;
> 
> We need to set a bnode handler for a world object. Should the procedure
> signature be
> 
> procedure Set_Handler (World: World_Type;
>                        Handler: access BNode_ID_Handler'Class);
> 
> or
> 
> procedure Set_Handler (World: World_Type;
>                        Handler: BNode_ID_Handler'Class);
> 
> The later can work with limited BNode_ID_Handler type, because tagged and
> limited types are always passed by reference.
> 
> I prefer the second one, because with the first procedure signature it is
> possible to pass an access to an already finalized object what is an
> error, but want to ask you opinion which of the two procedure signatures
> is better.

It may work like this (in fact it is more complex):

type World_Type is tagged limited
   record
      Handler: access BNode_ID_Handler'Class;
   end;

procedure Set_Handler (World: World_Type;
                       Handler: BNode_ID_Handler'Class) is
begin
   World.Handler := Handler'Unchecked_Access;
end;


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


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

* Re: Limited object by reference or access argument?
  2017-08-06 22:01 ` Victor Porton
@ 2017-08-06 23:21   ` Victor Porton
  2017-08-07  0:35     ` Victor Porton
  2017-08-07 23:31     ` Randy Brukardt
  0 siblings, 2 replies; 7+ messages in thread
From: Victor Porton @ 2017-08-06 23:21 UTC (permalink / raw)


Victor Porton wrote:

> Victor Porton wrote:
> 
>> Suppose we have
>> 
>> type World_Type is tagged limited private;
>> 
>> type BNode_ID_Handler is abstract tagged limited private;
>> 
>> We need to set a bnode handler for a world object. Should the procedure
>> signature be
>> 
>> procedure Set_Handler (World: World_Type;
>>                        Handler: access BNode_ID_Handler'Class);
>> 
>> or
>> 
>> procedure Set_Handler (World: World_Type;
>>                        Handler: BNode_ID_Handler'Class);
>> 
>> The later can work with limited BNode_ID_Handler type, because tagged and
>> limited types are always passed by reference.
>> 
>> I prefer the second one, because with the first procedure signature it is
>> possible to pass an access to an already finalized object what is an
>> error, but want to ask you opinion which of the two procedure signatures
>> is better.
> 
> It may work like this (in fact it is more complex):
> 
> type World_Type is tagged limited
>    record
>       Handler: access BNode_ID_Handler'Class;
>    end;
> 
> procedure Set_Handler (World: World_Type;
>                        Handler: BNode_ID_Handler'Class) is
> begin
>    World.Handler := Handler'Unchecked_Access;
> end;

I think it should be access parameter, because (as of Ada2005 and above) 
this allows passing null, what may be useful in some situations.

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

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

* Re: Limited object by reference or access argument?
  2017-08-06 23:21   ` Victor Porton
@ 2017-08-07  0:35     ` Victor Porton
  2017-08-07 23:31     ` Randy Brukardt
  1 sibling, 0 replies; 7+ messages in thread
From: Victor Porton @ 2017-08-07  0:35 UTC (permalink / raw)


Victor Porton wrote:

> Victor Porton wrote:
> 
>> Victor Porton wrote:
>> 
>>> Suppose we have
>>> 
>>> type World_Type is tagged limited private;
>>> 
>>> type BNode_ID_Handler is abstract tagged limited private;
>>> 
>>> We need to set a bnode handler for a world object. Should the procedure
>>> signature be
>>> 
>>> procedure Set_Handler (World: World_Type;
>>>                        Handler: access BNode_ID_Handler'Class);
>>> 
>>> or
>>> 
>>> procedure Set_Handler (World: World_Type;
>>>                        Handler: BNode_ID_Handler'Class);
>>> 
>>> The later can work with limited BNode_ID_Handler type, because tagged
>>> and limited types are always passed by reference.
>>> 
>>> I prefer the second one, because with the first procedure signature it
>>> is possible to pass an access to an already finalized object what is an
>>> error, but want to ask you opinion which of the two procedure signatures
>>> is better.
>> 
>> It may work like this (in fact it is more complex):
>> 
>> type World_Type is tagged limited
>>    record
>>       Handler: access BNode_ID_Handler'Class;
>>    end;
>> 
>> procedure Set_Handler (World: World_Type;
>>                        Handler: BNode_ID_Handler'Class) is
>> begin
>>    World.Handler := Handler'Unchecked_Access;
>> end;
> 
> I think it should be access parameter, because (as of Ada2005 and above)
> this allows passing null, what may be useful in some situations.

Another argument for access types is that if we switch to a non-limited 
type, copying the Handler object may not happen (and we should be sure that 
it is not copied by the compiler).

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

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

* Re: Limited object by reference or access argument?
  2017-08-06 21:16 Limited object by reference or access argument? Victor Porton
  2017-08-06 22:01 ` Victor Porton
@ 2017-08-07 14:10 ` Jere
  2017-08-07 23:28 ` Randy Brukardt
  2 siblings, 0 replies; 7+ messages in thread
From: Jere @ 2017-08-07 14:10 UTC (permalink / raw)


On Sunday, August 6, 2017 at 5:16:37 PM UTC-4, Victor Porton wrote:
> Suppose we have
> 
> type World_Type is tagged limited private;
> 
> type BNode_ID_Handler is abstract tagged limited private;
> 
> We need to set a bnode handler for a world object. Should the procedure 
> signature be
> 
> procedure Set_Handler (World: World_Type;
>                        Handler: access BNode_ID_Handler'Class);
> 
> or 
> 
> procedure Set_Handler (World: World_Type;
>                        Handler: BNode_ID_Handler'Class);
> 
> The later can work with limited BNode_ID_Handler type, because tagged and 
> limited types are always passed by reference.
> 
> I prefer the second one, because with the first procedure signature it is 
> possible to pass an access to an already finalized object what is an error, 
> but want to ask you opinion which of the two procedure signatures is better.
> 
> -- 
> Victor Porton - http://portonvictor.org

You can also just make it tagged (Ada95 to present) or denote the 
procedure parameter as aliased (Ada2012):

procedure Set_Handler (World: World_Type;
                        Handler: aliased in out BNode_ID_Handler'Class);

I am not sure of the limitations to that as it is a new feature I 
am less experienced with.


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

* Re: Limited object by reference or access argument?
  2017-08-06 21:16 Limited object by reference or access argument? Victor Porton
  2017-08-06 22:01 ` Victor Porton
  2017-08-07 14:10 ` Jere
@ 2017-08-07 23:28 ` Randy Brukardt
  2 siblings, 0 replies; 7+ messages in thread
From: Randy Brukardt @ 2017-08-07 23:28 UTC (permalink / raw)


I strongly prefer keeping "access" out of interfaces unless absolutely 
necessary. Claw only uses "access" in a couple of circumstances; the vast 
majority of routines use normal object parameter passing.

BTW,  "access" has to pass runtime accessibility information, and it's 
possible to trip over that when converting the parameter to some other type 
(causing Program_Error to be raised). That doesn't happen when using normal 
parameters (although you have to use 'Unchecked_Access if you need an access 
value).

                          Randy.

"Victor Porton" <porton@narod.ru> wrote in message 
news:om80vg$1a68$1@gioia.aioe.org...
> Suppose we have
>
> type World_Type is tagged limited private;
>
> type BNode_ID_Handler is abstract tagged limited private;
>
> We need to set a bnode handler for a world object. Should the procedure
> signature be
>
> procedure Set_Handler (World: World_Type;
>                       Handler: access BNode_ID_Handler'Class);
>
> or
>
> procedure Set_Handler (World: World_Type;
>                       Handler: BNode_ID_Handler'Class);
>
> The later can work with limited BNode_ID_Handler type, because tagged and
> limited types are always passed by reference.
>
> I prefer the second one, because with the first procedure signature it is
> possible to pass an access to an already finalized object what is an 
> error,
> but want to ask you opinion which of the two procedure signatures is 
> better.
>
> -- 
> Victor Porton - http://portonvictor.org 


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

* Re: Limited object by reference or access argument?
  2017-08-06 23:21   ` Victor Porton
  2017-08-07  0:35     ` Victor Porton
@ 2017-08-07 23:31     ` Randy Brukardt
  1 sibling, 0 replies; 7+ messages in thread
From: Randy Brukardt @ 2017-08-07 23:31 UTC (permalink / raw)


"Victor Porton" <porton@narod.ru> wrote in message 
news:om88a9$1kis$1@gioia.aioe.org...
...
>> It may work like this (in fact it is more complex):
>>
>> type World_Type is tagged limited
>>    record
>>       Handler: access BNode_ID_Handler'Class;
>>    end;
>>
>> procedure Set_Handler (World: World_Type;
>>                        Handler: BNode_ID_Handler'Class) is
>> begin
>>    World.Handler := Handler'Unchecked_Access;
>> end;

If you had written instead:

procedure Set_Handler (World: World_Type;
                        Handler: access BNode_ID_Handler'Class) is
begin
    World.Handler := Handler; -- (1)
end;

Program_Error will be raised at (1) if you passed a local object as a 
handler. Bob Duff calls that a "tripping hazard", and I agree. It's the sort 
of thing that gets by testing and only breaks at the worst possible time. 
Avoid anonymous access types!!

                                        Randy.


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

end of thread, other threads:[~2017-08-07 23:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-06 21:16 Limited object by reference or access argument? Victor Porton
2017-08-06 22:01 ` Victor Porton
2017-08-06 23:21   ` Victor Porton
2017-08-07  0:35     ` Victor Porton
2017-08-07 23:31     ` Randy Brukardt
2017-08-07 14:10 ` Jere
2017-08-07 23:28 ` Randy Brukardt

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