[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | boos@g... |
| Subject: | Re: modules local to functions. |
> I have a functor such as follows:
>
> module type Specifyn = sig val n : int end;;
>
> module Makespecific(N:Specifyn) = struct
> type mytype = int
> let mirror () : mytype = N.n
> end;;
>
> I can apply it at the main level like:
>
> module Fred = Makespecific(struct let n = 7 end);;
>
> However, there are cases when I want to define a module in the middle
> of a function (eg a functor of packed binary arrays where I want to (...)
Hello,
I have encountered a similar problem yesterday!
My understanding is that YOU CAN'T do that in CSL at all: functor
application means code generation, and there's no runtime code
generation!
One way to circumvent this problem is to change slightly the semantic
of your argument module, for example:
module type Specifyn = sig val n : unit -> int end
^^^^^^^
This should be not too inconvenient, and almost as simple to use as
yours:
module Makespecific(N:Specifyn) = struct
type mytype = int
let mirror () : mytype = N.n ()
^^
end
let toto_fred_n = ref 10
module Fred = Makespecific(struct let n () = !toto_fred_n end)
let toto digits =
toto_fred_n := digits;
Fred.mirror ()
(This particular solution may even be expressed simpler:
module type Specifyn = sig val n : int ref end
^^^
module Makespecific(N:Specifyn) = struct
type mytype = int
let mirror () : mytype = !N.n
^
end
module Fred_N = struct let n = ref 10 end
module Fred = Makespecific(Fred_N)
let toto digits =
Fred_N.n := digits;
Fred.mirror ()
)
Hope it helps!
Christian