[comp.lang.misc] Ada Generics

chl@r1.uucp (Charles Lindsey) (08/09/89)

In article <6204@hubcap.clemson.edu> billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu writes:
    (speaking about how difficult it was to program in Pascal, as opposed to Ada)
>   ..........................................................  What about
>   the joy of writing code to manipulate a linked list for the 348th time
>   because you can't express it cleanly, once and for all, using generics?
>
But how DO you manipulate linked lists in Ada using Generics?

Suppose I want to write a simple procedure to find the last element of a
linked list. I try the following:

generic
	type T is private;
	type LINK;
	type PLINK is access LINK;
	type LINK is record
		VAL: T;
		NEXT: PLINK;
		end record;
function FIND_LAST(LIST: PLINK) return PLINK is
declare
	TEMP: PLINK := LIST;
begin
	if LIST=null then return null
	else	while TEMP.NEXT/=null
		loop TEMP := TEMP.NEXT
		end loop;
		return TEMP;
	end if;
end FIND_LAST;

BUT there are at least two syntactic impossibilities in the generic_part of
that. And even if it had been legal, it would have been a pain to instantiate.

The odd thing is that, if the problem had been to find the last element of an
array, it would have been easy.

Or can someone show me that there IS a way to write FIND_LAST in Ada?

Charles Lindsey
cs.man.ac.uk

marc@hpfcdc.HP.COM (Marc[e] Sabatella) (08/16/89)

>I don't know.  I tend to laugh when I get a non-trivial (and usually
>incomplete) spec for a trivial program, which is what most of the
>beginning CS programs tend to have.  It makes ADTs *seem* like useless
>overhead (they may not be, especially on larger projects, but tiny
>projects aren't the way to convince someone that ADTs are worth their
>cost).

Agreed.  But apart from the need to tell a student to include the "magic"
lines that give him access to I/O, I am in no way convinced that toy Ada
programs are inherently more difficult to write than toy Pascal programs.
The advantage I see, is that in *followup* courses, one could teach ADT's,
exception handling, concurrecny, software engineering, etc. without having
to switch languages, if the original language was Ada.

By the way, we had a particularly evil prof who forced ADT's on us by having
half the class write specs, the other half do implementations, and then played
mix and match with the results.

--------------
Marc Sabatella
HP Colorado Language Lab
marc%hpfcrt@hplabs.hp.com

marc@hpfcdc.HP.COM (Marc[e] Sabatella) (08/17/89)

#We forgot the order of the parameters and swapped them. But does
#the compiler complain? No, integer to integer, perfectly OK. You
#call that stronly typed? I don't. In Ada you can say:

>	You yourself said that the above example would produce an error, granted
>	I don't know whether you mean logical or compiler

He meant what he said - the complier does not complain, but the code is in fact
wrong.  So rather than finding your mistake at compile time, where the compiler
can tell you the line number the error occurred on, you are forced to run the
program, maybe notice the results are wrong, and then debug it.  That is why
some people consider it *good* when a compiler is able to find more of your
errors, which is why these people like strong type checking.


#  Add to this private types, limited private types and you already
#have more devices in Ada than Pascal to enforce real data abstraction.

>	Ok, but why does a *beginning* level student need them?

No, but when he takes the next course, he doesn't have to switch languages.

>	I am not surehow you define debug, but I define it as fixing the logical
>	problems.  So, if you spend more time figuring out syntax errors with
>	Ada and same time for logical errors with Ada and Pascal, which one
>	takes more time to write a program in?

No, the point is that some things that qualify only as "logical" errors in
Pascal (ie, the apples and oranges type mismatch example above) and can be
discovered earlier in Ada.  A beginning programmer is likely to make the same
number of mistakes in either language; in Ada, a higher percentage are
discovered for you by the compiler.

The fact that *you* spent a long time trying to deal with syntax error
messages in Ada says more about you than about the language.  No, not that
you are stupid, but that you are probably approaching the problem from a
Pascal standpoint, and are falling on Pascal-ism's which don't work in Ada.

>	Now, you'll tell me that Pascal cannot
>	be split?

No, it can't.  Read the language definition some time.
Now, it is true that several 'real world' compilers (most, perhaps) allow it.
Most don't do as much type checking as they could have with one source file,
or as much as Ada could with its eyes closed and one hand tied behind its back.

>	In one of my classes, we had an asignment - write *4* *seperate*
>	*modules* in Pascal that would each do a specific thing.

No, you had an assignment to write four separate modules in a nonstandard
Pascal variant.  Which is useless in the "real world", or on a different
machine in school, or when you transfer to a different school, etc. (they will
each have a different nonstandard Pascal variant).  Unrelated to the issue of
can you teach abstraction, modularity, and reusability in Pascal, but yet
another strike against it - Pascal learned and written on one system is useless
on another.  This doesn't have much of a bearing on education, though, unless
you advocate that students should learn the language they will use in the real
world, in which case, the sad truth is still "learn COBOL, Fortran, and/or C".

--------------
Marc Sabatella
HP Colorado Language Lab
marc%hpfcrt@hplabs.hp.com