F# interface/abstract 类型和序列化

F# interface/abstract type and serialization

我正在使用 orleankka 在 orleans 框架之上编写一个小型机器学习引擎。我需要父子 class 关系,其中父支持获取、设置、默认构造函数和序列化。我在 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

      }

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

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

type Iobject() =

应该是

type Iobject =

我已经使用接口修改了 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"
    }