comp.lang.ada
 help / color / mirror / Atom feed
* Object-Oriented style question
@ 2012-01-08 12:45 Georg Bauhaus
  2012-01-08 12:52 ` Simon Wright
                   ` (4 more replies)
  0 siblings, 5 replies; 18+ messages in thread
From: Georg Bauhaus @ 2012-01-08 12:45 UTC (permalink / raw)


Given a tagged O-O type T in package Pak and a "method" of T,
called "Info". Method Info queries the state of its parameter,
which is of type T. States are of a type named "Value", which
also happens to be defined in Pak.

    function Info (Item : in T) return Value;
           -- queries status of Item

The primitive operation Info may be inherited when deriving
another type from Pak.T. A program that calls Info in an O-O style
may want the call to be dispatched to the inherited operation.

A client working with the T hierarchy could look like this:

    procedure Get_Info (Item : Pak.T'Class) is
    begin
       Status := Item.Info;  -- dispatching call
    end Get_Info;

where

    Status : Pak.Value;

My question is about the best way to declare the Info operation,
and its parameter profile in particular.

(A follow-up question is whether introduction to O-O programming
in Ada should be careful when mentioning plain "access" parameters.)

There is a number of alternative possibilities of declaring query
functions:

package Pak is

    type Value is range 1000 .. 1003;

    type T is abstract tagged private;

    function Info (Item : in T) return Value;
    function Info_1 (Item_Doubly_Indirect : access T) return Value;
    function Info_2 (Item_Doubly_Indirect : access constant T) return Value;
    function Info_3 (Item_Doubly_Indirect : not null access constant T) return Value;

private
    type T is tagged record
       Data : Value := Value'First;
    end record;
end Pak;

When evaluating the alternative possibilities of declaring
a query function such as Info, Info_1, Info_2, and Info_3---and
ignoring the fact that in some cases there might be technical  or other
issues that narrow the choices---I am thinking that the operations
Info and Info_3 are almost the same, insofar as the argument is
*read-only* in both cases, and the argument must be an *existing*
object in both cases.

Info_2 is still close to the meaning of a query function except
that the parameter may be null. This then means that there is *no*
object of type T'Class and, consequently, there cannot be a state!
Programs that call Info_2 could then needlessly raise an exception.
(If "null" is somehow important in a set of objects from
the T hierarchy, this does not preclude testing for "null",
since the test can be performed at the place in the program
where both the null pointer and the test are needed (and a default
state value assigned).)

Info_1 does not even express, at the language level, that Info_1
is a query: the query function has *write* access to the state
information. Arguably, this can be an invitation to introduce
obscure errors.

The above definitions are not even informally equivalent, then,
and, in particular, Info and Info_1 are the least alike.

Still, some recent program fragments seem to indicate that programmers
approaching O-O programming in Ada use Info_1 (with "access") for query
functions, IMHO the least preferable choice in the subjective evaluation
hierarchy above.

My guess is that the choice is caused by transporting knowledge acquired
when using a language that is different from most O-O languages,  since
most O-O languages do *not* use *explicit* "access" for declaring
O-O "methods" (Ada, Eiffel, Java, C#, OCaml, ...). Not even for
dispatching calls.  They have O-O mechanics built into the language
and translation helps with keeping O-O mechanics correct, and efficient.

I have effectively two questions, then, one concerning didactic
style and one concerning cost and robustness of using Ada:

First question: Will it be preferable if those introducing O-O
in Ada use a (virtual) rubber eraser and clean up introductions
by removing unneeded (and in some cases dangerous) "access" from
declarations of O-O primitive operations?

Second question: Technically, will it be advisable to work on
"access-free" bindings to O-O libraries because the Ada language
makes O-O types be by-reference as is?  Or on facilitating these
with the help of a compiler matching by-reference  mechanics of
foreign languages?



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

end of thread, other threads:[~2012-02-08 12:39 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-08 12:45 Object-Oriented style question Georg Bauhaus
2012-01-08 12:52 ` Simon Wright
2012-01-08 13:25   ` Dmitry A. Kazakov
2012-01-08 14:18 ` Robert A Duff
2012-01-08 20:32   ` Martin Dowie
2012-01-08 20:52     ` Robert A Duff
2012-01-09 22:34   ` Adam Beneschan
2012-01-09 23:21     ` Robert A Duff
2012-01-10  8:34     ` Dmitry A. Kazakov
2012-01-09  8:55 ` Maciej Sobczak
2012-01-09 23:58   ` Georg Bauhaus
2012-01-10  8:47     ` Maciej Sobczak
2012-01-10 10:27       ` Dmitry A. Kazakov
2012-01-10 12:27       ` Georg Bauhaus
2012-01-11  8:54         ` Maciej Sobczak
2012-01-10 21:26       ` Randy Brukardt
2012-02-08 12:23 ` Yannick Duchêne (Hibou57)
2012-02-08 12:39 ` Yannick Duchêne (Hibou57)

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