[net.lang.mod2] variable-length dynamic objects

g-frank@gumby.UUCP (01/30/85)

> From: sher
> 
> Sometime in the recent past I had read on the net how modula 2 was a strongly
> typed language but flexible enough for system programming and one
> which had data encapsulation built into the language.  I thought "How
> neat! just the language to do image hacking in!"  This did not work.
> I ran immeadiately into this problem.  I needed to store images in a
> data structure.  This data structure should be able to store any image
> whose elements were of the right type.  Thus I started to write:
> TYPE Image = RECORD
> 	number_cols : CARDINAL;
> 	number_rows : CARDINAL;
> 	image : ???
> 
> 
> There is no way to have a streach array in MODULA 2!  The only
> variable size data structure is a linked list!  (Only a lisp hacker
> would store an image in a linked list).  Don't tell me about
> extensions to the language in various implementations.  If I have to
> extend the language to do something trivial like this then that
> language is worthless!
> 
> Note I have just speant 8 hours trying to get some code working that 
> trys to allocate arrays whose size is only known at runtime.  I tried
> to use the supplied loopholes and even tried using a C allocation
> routine.  I am now giving up on this worthless language and will code
> from now on in C happy in the knowledge that there still is not another
> supported (at least here) language that is usable.
> 
> (Another hole in Modula 2 that have been commented on are that
> one can not have constants whose values are hidden (not from the code
> but from the code writer).  
> 
> -David Sher

I can sympathize with your frustration in this case.  Learning a new way
to approach a problem in a new language can cause these kinds of sentiments
in the best of programmers.

However, I would like to draw your attention to page 161 of the second edition
of Wirth's book:

	The type ADDRESS is defined as

		ADDRESS = POINTER TO WORD
	
	It is compatible with all ponter types, and also with the type CARDINAL.
	Therefore, all operators for integer arithmetic apply to operands of
	this type.  Hence, the type ADDRESS can be used to perform address
	computations and to export the results as pointers.

Also note on page 158 the existence of the procedure ALLOCATE from module
Storage, which takes a var address parm and a cardinal size, and allocates
a module of the requested size.  Your record, as described above, can contain
the horizontal and vertical sizes, and a variable of type ADDRESS, pointing
to a buffer allocated in this fashion, and functions may be defined to ref-
erence locations within this image array in any way you wish, with the caveat
that no array bounds checking will be possible.

Actually, I like the idea of arrays whose sizes may be determined at run time.
You won't find them in C, though.  Try Ada.


-- 
      Dan Frank

	"good news is just life's way of keeping you off balance."

rap@oliven.UUCP (Robert A. Pease) (02/03/85)

>> . . . .  Thus I started to write:
>>
>> TYPE Image = RECORD
>> 	number_cols : CARDINAL;
>> 	number_rows : CARDINAL;
>> 	image : ???
>> 
>> 
>> There is no way to have a streach array in MODULA 2!  The only
>> variable size data structure is a linked list!
>> 
>> -David Sher
>
>However, I would like to draw your attention to page 161 of the second edition
>of Wirth's book:
>
>	The type ADDRESS is defined as
>
>		ADDRESS = POINTER TO WORD
>	
>Also note on page 158 the existence of the procedure ALLOCATE from module
>Storage, which takes a var address parm and a cardinal size, and allocates
>a module of the requested size.  Your record, as described above, can contain
>the horizontal and vertical sizes, and a variable of type ADDRESS, pointing
>to a buffer allocated in this fashion, and functions may be defined to ref-
>erence locations within this image array in any way you wish, with the caveat
>that no array bounds checking will be possible.
>-- 
>      Dan Frank

This tends to illustrate something that I have felt for a  long  time.
People  who  are  used  to  (and  like!) another programming language,
especially C programmers, tend to not like Modula-2 because it behaves
differently from what they are used to.  They cannot easily see how to
perform a specific task and think that it cannot be done  in  Modula-2
at all.
This is not to say that there is a competition only  between  Modula-2
programmers  and C programmers.  It exists between all programmers who
have become very familiar with a particular language.  It even  exists
between users of different editors.  I wonder if it may have something
to do with the tendency of people to resist change?
By  the  way,  Modula-2  can  support  dynamic  allocation  of  string
variables  if  the programmer doesn't insist on overloading operators.
See the example above for a hint how.
-- 

					Robert A. Pease
    {hplabs|zehntel|fortune|ios|tolerant|allegra|tymix}!oliveb!oliven!rap