[comp.lang.modula3] Instance initialization in Modula-3?

janssen@parc.xerox.com (Bill Janssen) (01/24/91)

When defining an OBJECT type in Modula-3, how does one specify the
initialization routine to be called from inside NEW?  And is there
any way to control allocation on a class basis, or does NEW always
create new objects on the heap?

Bill

--
 Bill Janssen        janssen@parc.xerox.com      (415) 494-4763
 Xerox Palo Alto Research Center
 3333 Coyote Hill Road, Palo Alto, California   94304

muller@src.dec.com (Eric Muller) (01/25/91)

In article <JANSSEN.91Jan23221159@holmes.parc.xerox.com>, Bill Janssen asks:

> When defining an OBJECT type in Modula-3, how does one specify the
> initialization routine to be called from inside NEW?

Modula-3 does not allow you to attach to an object type code to
executed when an object of that type is created.

The only thing you can do in the type declaration is to specify
default initial values for the fields, which must be constant
expressions, and default methods. 

>  And is there any way to control allocation on a class basis, or
> does NEW always create new objects on the heap?

NEW always create new objects on the traced or untraced heap
(depending on the object type).

Eric Muller.

gnelson (Greg Nelson) (01/25/91)

The convention used in Trestle (the Modula-3 window toolkit) is that
each object type has an "init" method that returns the object after
initializing it.  So you can type

    VAR v := NEW(T).init(args)

to allocate and initialize an object of type T. If T has a supertype
S, and S uses the same convention, then T.init typically contains the
call EVAL(S.init(self)), to initialize the supertype.

This style won't work with SRC Modula-3 1.6, which doesn't let you
NEW an opaque type; but it will work soon.  There is more discussion
about this issue in the "twelve changes to Modula-3" message.

Greg

pierson@encore.com (Dan L. Pierson) (01/25/91)

Regarding Re: Instance initialization in Modula-3?; gnelson (Greg Nelson) adds:

> The convention used in Trestle (the Modula-3 window toolkit) is that
                         ^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Hmmm, interesting, please post more info.
--

                                            dan

In real life: Dan Pierson, Encore Computer Corporation, Research
UUCP: {talcott,linus,necis,decvax}!encore!pierson
Internet: pierson@encore.com

krey@i30fs1.ira.uka.de (Andreas Krey) (01/26/91)

In article <1991Jan24.122222.9214@src.dec.com>, muller@src.dec.com (Eric
Muller) writes:
> In article <JANSSEN.91Jan23221159@holmes.parc.xerox.com>, Bill Janssen asks:
> 
> > When defining an OBJECT type in Modula-3, how does one specify the
> > initialization routine to be called from inside NEW?
> 
> Modula-3 does not allow you to attach to an object type code to
> executed when an object of that type is created.
>

So why not? For me C++'er it's lot of a reason not to use modula-3.
 

.signature: No such file or directory

gnelson (Greg Nelson) (01/26/91)

Dan L. Pierson asks about the Trestle window toolkit.  

Trestle is a Modula-3 toolkit for the X window system.  It will be
released for beta-test in February.  There is an extensive tutorial
on Trestle in Chapter 7 of "Systems Programming in Modula-3", which
is now being printed by Prentice Hall.

gnelson (Greg Nelson) (01/26/91)

Andreas Krey asks why Modula-3 does not automatically invoke an
initialization method when an object is allocated.  This issue is
discussed in the "twelve changes" message sent to this bboard a month
ago (19 Dec), and also more briefly in my message of 12 Jan.

janssen@parc.xerox.com (Bill Janssen) (01/29/91)

In article <1991Jan24.122222.9214@src.dec.com> muller@src.dec.com (Eric Muller) writes:

   Modula-3 does not allow you to attach to an object type code to
   executed when an object of that type is created.

Yes.  As a workaround, I've decided to create a base class that
defines Make(), which is then the new "standard" way of creating an
instance.  Make() will call the standard methods Allocate() and
Initialize(), which can then be appropriately overridden by the
subclass implementor.

Thanks.

Bill
--
 Bill Janssen        janssen@parc.xerox.com      (415) 494-4763
 Xerox Palo Alto Research Center
 3333 Coyote Hill Road, Palo Alto, California   94304

muller@src.dec.com (Eric Muller) (01/29/91)

In article <JANSSEN.91Jan28154140@holmes.parc.xerox.com>, janssen@parc.xerox.com (Bill Janssen) writes:

> Yes.  As a workaround, I've decided to create a base class that
> defines Make(), which is then the new "standard" way of creating an
> instance.  Make() will call the standard methods Allocate() and
> Initialize(), which can then be appropriately overridden by the
> subclass implementor.

I am not sure I understand your solution. I believe that you will have
some problems with the type declarations and/or have to do some
(maybe implicit) narrowing. Can you tell more ?

Eric.