singetons in functors
Torsten Anders
t.anders at qub.ac.uk
Thu Oct 28 15:37:41 CEST 2004
On Thu, 2004-10-28 at 13:35, Maarten van den Dungen wrote:
> Inside a functor some exported function returns a record with a unique feature (created with the NewName
> function). However when the functor is loaded indepedently in different
> parts in the program, the code inside the functor is executed each time
> the functor is loaded, giving different feature names.
I had a similar problem and got help on this list (old alswer by
Christian Schulte pasted below).
Short answer: execute the name creation in a 'prepare' section of the
functor.
BTW: Is this 'prepare' section documented somewhere?
Best,
Torsten
> // code inside the functor
> functor
> export
> New GetStream
prepare
> PStreamTag = {NewName}
> define
>
> fun {New}
> !!{NewChunk mstream(PStreamTag:{NewCell _})}
> end
>
> fun {GetStream P}
> !!{Access P.PStreamTag $}
> end
> end
On Tue, 2004-07-13 at 09:54, Christian Schulte wrote:
> Torsten,
>
> yes it is indeed intended behaviour. A functor is implemented in terms of a
> procedure. When the functor is loaded and requested, the procedure
> containing the functor's body is executed and returns as a result a module
> (a record with your specified export as features). So in short, everytime
> you load the functor defining MyName you'll get a new name!
>
> The way to avoid that is to have a functor that uses the "prepare" section.
> This section (constructed exactly like the body of a functor in the "define"
> section) is executed only once when the functor is compiled. So you can put
> the definition of MyName in that section and make it available via a
> feature, say myName. Each time you load that functor you get the very same
> name under the feature myName.
>
> As an example for defining a functor doing that
>
> functor
> export
> MyName
> prepare
> MyName = {NewName}
> end
>
> Now, the following shows true in the Inspector:
>
> declare [M1]={Module.link ['test.ozf']}
> declare [M2]={Module.link ['test.ozf']}
>
> {Inspect M1.myName == M2.myName}
>
>
> Cheers
> Christian
>
> --
> Christian Schulte, http://www.imit.kth.se/~schulte/
>
> -----Original Message-----
> From: mozart-users-bounces at ps.uni-sb.de
> [mailto:mozart-users-bounces at ps.uni-sb.de] On Behalf Of Torsten Anders
> Sent: Monday, July 12, 2004 3:31 PM
> To: users at mozart-oz.org
> Subject: Functors and names
>
>
> Hi,
>
> I defined some class hierarchy. To be able to do a check whether an
> arbitrary datum is an instance of any class of this hierarchy I use a
> name at a feature of the top-level class (as recommended somewhere in
> the Oz lit.).
>
> class MyClass
> feat !MyName: unit
> <class body>
> end
> fun {IsMyObject X}
> {Object.is X} andthen {HasFeature X MyName}
> end
>
> My classes, the type checking function, and the name for type checking
> is defined in functor A. Functor A also defines some function X to
> generate more complex instances of my classes by calling the init
> methods of the classes. In the Oz-IDE (i.e. not in any functor) I
> define a function Y which calls function X. Everything works fine.
>
> Now, I move function Y into a functor B and this breaks the
> typechecking scheme. The objects created by function Y still contain a
> feature with some name, but the name is different then the name defined
> for type checking in functor A.
>
> I do not understand this behaviour: is this intended (e.g. for security
> reasons?)
>
> Thank you!
>
> Best,
> Torsten
--
Torsten Anders
Sonic Arts Research Centre
Queen's University Belfast
Tel: +44 28 9097 4761 (office)
+44 28 9066 7439 (private)
www.torsten-anders.de
More information about the mozart-users
mailing list