1

我正在使用 orleankka 在 orleans 框架之上编写一个小型机器学习引擎。我需要一个父子类关系,其中父级支持获取、设置、默认构造函数和序列化。我的尝试在 F# 中失败了。

更新:使用接口现在我只需要弄清楚图像通道对象的序列化。

type imagechannel = int * int * char array[][]


type Iobject =
    abstract Value : obj with get, set
    abstract FromSerial : SerializationInfo -> StreamingContext -> unit
    abstract ToSerial : SerializationInfo -> StreamingContext -> unit

type ImageChannel() =
    let mutable value : option<imagechannel> = None
    interface Iobject with
        member this.Value with get() = value :> obj and set v = value <- v :?> option<imagechannel>
        member this.FromSerial info context =

        member this.ToSerial info context =

上下文代码:

type ProcessorMessage =
    | Eval of (Iobject -> Parms -> Iobject) * Parms
    | New of Iobject
    | Value
    | Load of cache
    | Save of cache
    | Trans of string * (Iobject -> Parms -> Iobject) * Parms

type Processor() =
    inherit Actor<ProcessorMessage>()

    let mutable value :option<Iobject> = None

    override this.Receive message reply = task {
      match message with
          | Eval(fn,p) -> value <- (fn value p)
          | Load(cache) -> //deserialize value
          | Save(cache) -> //serialize value
          | New(v) -> value <- v
          | Value -> reply value
          | Trans(addr,fn,p) -> let proc = this.System.ActorOf(addr)
                                proc <! New (fn value p) |> ignore

      }

我应该直接实现序列化接口吗?如何用不同类型覆盖抽象值成员?还有其他建议吗?

4

2 回答 2

0

我认为您不希望在接口定义中使用括号。

type Iobject() =

应该

type Iobject =
于 2015-06-21T08:38:07.317 回答
0

我已经使用接口修改了 Hello World 示例http://tinyurl.com/pgxs6nv,并且序列化工作正常。你能给我们更多关于错误的信息吗?

type imagechannel = int * int

type IObject =
    abstract Value : obj with get, set

type ImageChannel() =
    let mutable value : option<imagechannel> = None    
    interface IObject with
      member this.Value with get() = value :> obj and set v = value <- v :?> option<imagechannel>

type ImageChannel2() =
    let mutable value : string = ""    
    interface IObject with
      member this.Value with get() = value :> obj and set v = value <- v :?> string

type Message = 
   | Greet of string
   | Hi
   | New of IObject

type Greeter() = 
   inherit Actor<Message>()   

   override this.Receive message reply = task {
      match message with
      | Greet who -> printfn "Hello %s" who
      | Hi -> printfn "Hello from F#!"
      | New iObj -> printfn "Obj %s" (iObj.Value.ToString())
   }

[<EntryPoint>]
let main argv = 

    printfn "Running demo. Booting cluster might take some time ...\n"

    use system = ActorSystem.Configure()
                            .Playground()
                            .Register(Assembly.GetExecutingAssembly())
                            .Done()

    let actor = system.ActorOf<Greeter>(Guid.NewGuid().ToString())
    let imgChannel = ImageChannel() :> IObject
    imgChannel.Value <- Some(5,1) :> obj    

    let imgChannel2 = ImageChannel2() :> IObject
    imgChannel2.Value <- "asdasdasd" :> obj    

    let job() = task {
      do! actor <! New imgChannel
      do! actor <! New imgChannel2
      do! actor <! Hi
      do! actor <! Greet "Yevhen"
      do! actor <! Greet "AntyaDev"
    }
于 2015-07-19T17:51:17.417 回答