对象内的模块

时间:2014-04-23 14:11:21

标签: ocaml

我想通过这个对象的类型在对象中定义一个参数化模块。

我有两个模块互锁:

(* Parameters*)
module type A = sig
        type agent
        type intern_agent = { i : agent}
        val create : agent -> intern_agent
end

module type E = sig
        type event
end



module  type StateType = sig

  type agent
  type event

  type  state_t = { 
    mutable name : string;
    mutable parentstate  :  state_t option;
  }
end


module State (A : A) (E : E) = struct

        type agent = A.agent
        type event = E.event
    type  state_t = { 
            mutable name : string;
             mutable parentstate  :  state_t option;
    }


    (*...*)
end


module Agent (S : StateType) =
        struct
          type agent = S.agent
          type event = S.event
          type state_t = S.state_t

      type  agent_t = {
          mutable agent      : agent ;
      }

      let create a1   = {
          agent          = a1;
      }
end

(* An implementation of E*)
type event1 = Event1 | Event2;;
module E = struct type event = event1 end;;

我想做的是这样的(语法不正确,但代表我想做的事):

class  character = object (self :'self)
 val mutable position        = (0,0)
 val agent                         =
                let A = (module Ag  = struct
                                type agent = 'self
                                type intern_agent = { i : agent}
                                let create a = { i = a }
                              end)
                     in
                let Ag = (module Agent(State(A)(E)) ) in
                Ag.create self

 method getPosition          = position

end;;

如何编写此代码以便能够定义一个由自身参数化的代理值?

谢谢

1 个答案:

答案 0 :(得分:0)

字符类定义的语法正确版本是

class  character = object (self :'self)
  val mutable position        = (0,0)
  val agent =
    let module A = struct
      type agent = character
      type intern_agent = { i : agent}
      let create a = { i = a }
    end in
    let module Ag = Agent(State(A)(E)) in
    Ag.create self

  method getPosition          = position
end 

虽然它不会编译,因为agent_t将逃避值代理定义的范围。实际上,您要求编译器允许您从函数中返回一个类型为local的函数。

我希望我能就如何重新设计代码给你一些建议,但我需要更多关于你的意图的信息。