[英]F# interface/abstract type and serialization
我使用orleankka在orleans框架之上編寫了一個小的機器學習引擎。 我需要父級子類關系,其中父級支持get,set,默認構造函數和序列化。 我的嘗試在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"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.