comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Tagged type more type safe than access to subprogram ?
Date: Wed, 28 Oct 2009 09:55:08 +0100
Date: 2009-10-28T09:55:08+01:00	[thread overview]
Message-ID: <qq21l96578k3.2h37o6ermxsm.dlg@40tude.net> (raw)
In-Reply-To: b1b16af6-8274-4b81-a993-9d8dd582ec74@k19g2000yqc.googlegroups.com

On Wed, 28 Oct 2009 00:51:19 -0700 (PDT), Hibou57 (Yannick Duchêne) wrote:

> Hello Ada Artists,
> 
> I was reading the Ada 95 Quality and Style Guide, seeking for some
> inspiration about a design / style doubt.
> 
> I came into Chapter 5, “ CHAPTER 5: Programming Practices ”,
> 
> 5.3.4 Subprogram Access Types says :
>> You can achieve the same effect as access-to-subprogram types for
>> dynamic selection by using abstract tagged types. You declare an
>> abstract type with one abstract operation and then use an
>> access-to-class-wide type to get the dispatching effect. This technique
>> provides greater flexibility and type safety than access-to-subprogram types
> Here : http://www.iste.uni-stuttgart.de/ps/ada-doc/style_guide/sec_5a.html#5.3.4
> 
> I agree about the “ greater flexibility” (I've recently meet such a
> case), but I do not understand the “ and [greater] type safety ”.
> 
> If it's Ok for me to assert that tagged type is a more flexible way
> than access to subprogram, I do not see a case where access to
> subprogram would be less type safe than tagged type.
> 
> If there is something I do not understand, this may means I have
> something to learn about it (the purpose of the question then).
> 
> Does any one know a case which match this assertion ?

An access to subprogram is a poor-man's closure. Let's ignore "access" part
and consider a pure downward closure (as it should have been in Ada).

I argue that a helper type with an abstract primitive subprogram is safer
than a closure, both safer and type-safer.

The general safety comes from the fact that a closure brings a context with
it, which an object normally does not. An abstract type is better
encapsulated and there is less chances to run into occasional side effects.
Even if the side effects are desired I would argue that it is better and
safer to limit them to the object rather than to the closure's context. The
crucial point is that closure's effects are "there", while effects on the
object are localized to the object's state, there are "here", at the call
point.

The type safety is gained through different types derived from the abstract
parent. There might be no difference at the caller side, e.g. whether you
mistakenly call to the closure C1 instead of C2, or mistakenly pass an
instance of S1 instead of S2 (both from the S'Class). Yet it is type safer
with regard to the parameters required to construct S2. Its constructor can
have a different signature, so that the parameters you pass in order to
create it were different from S1.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2009-10-28  8:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-28  7:51 Tagged type more type safe than access to subprogram ? Hibou57 (Yannick Duchêne)
2009-10-28  8:55 ` Dmitry A. Kazakov [this message]
2009-10-29 11:00   ` Hibou57 (Yannick Duchêne)
2009-10-29 17:54     ` Dmitry A. Kazakov
2009-10-29 20:45       ` Georg Bauhaus
2009-10-30  8:25         ` Dmitry A. Kazakov
replies disabled

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