[M3devel] small array in modula-3?

Jay jay.krell at cornell.edu
Mon Nov 12 01:51:55 CET 2007


What is the right way to have a variably sized but always small array in Modula-3?My array will only ever have 1 or 2 elements.I'd like to always allocate room for 2 elements, and have there be a size.
It seems I have a choice of
a) an "open" arrayb) wrap it up in a record
I'd like so have, like:
TYPE A = ARRAY [0..1] OF FOO;
And be able to say:
VAR a : A;
 .. a.size FOR i := 0 TO a.size DO   do something with a[i]
It seems I have no option but, like:
TYPE A = RECORD  a: ARRAY[0..1] OF FOO;  size := 1; (* usually of size 1, sometimes 2 *)END
and then FOR i := 0 TO a.size DO  do something with a.a[i];
That is "ok". Not great -- what to call the inner a?
But then furthermore, I'd like to construct constants.It seems I am stuck with either the verbose:
PROCEDURE F(a:A);
F( A { ARRAY[0..1] OF FOO { .. } );
OR I have to come up with a name for the embedded array.But of course, its type is really "the same" as the outer type.
To wit:
TYPE FooArray = ARRAY[0..1] OF Foo;
TYPE FooArray = RECORD  a : FooArray;  size := 1;END;
F( FooArray { FooArray { .. } );
But I have to come up with different names.
And I don't think I can leave out the name for the constructor, like:
F( FooArray { { .. } );
Though that might seem nice.
Am I understanding everything?
Have folks hit this before and there's a set of names that don't seem too lamethat folks use?
 
Also -- language documentation?Nelson's green book is excellent.The stuff in the doc directory is very dry and scientific.The tutorial seems more like a reference. Or maybe I didn't read it enough.Is there better, in case I need a refresher?I think I got it, via Nelson's book from memory (wonder if I can find mine..) and the reference,but I don't think anyone could learn from the current online docs in the source tree.
Maybe it's me, some combination of laziness and short attention span as I age..
 
Btw, C doesn't offer a great solution here, though it offers leaving out some type names.In C++ I could have both .size and operator[].Modula-3 seems to be missing operator overloading.It's got some builtin stuff, even array assignment and equality and I think  even record assignment and equality, but it is still a bit limiting.
Also, if I understand things correctly, this has long bothered me about Modula-3, though I'm more tolerant now -- Modula-3 doesn't seem to allow for lighter wieght objects. Like, small stack allocated structs with member functions. You seem to have to chose between heap allocated garbage collected virtual member functions full featured objects, or featureless dumb structs. It's nice how C++ allow hybrids -- objects don't have to be heap allocated and member functions don't have be virtual. Or am I missing something?
Anyway, I've gotten to accept C a bit more vs. C++ so I can deal witht: Type;Type_DoSomething(t);
in place of:t.DoSomething();
 
 - Jay
_________________________________________________________________
Boo! Scare away worms, viruses and so much more! Try Windows Live OneCare!
http://onecare.live.com/standard/en-us/purchase/trial.aspx?s_cid=wl_hotmailnews
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20071112/9e13a7a5/attachment-0001.html>


More information about the M3devel mailing list