[英]F# interface/abstract type and serialization
I am writing a little machine learning engine on top of the orleans framework with orleankka. 我使用orleankka在orleans框架之上编写了一个小的机器学习引擎。 I need a parent child class relationship where the parent supports get, set, default constructor and serialization. 我需要父级子类关系,其中父级支持get,set,默认构造函数和序列化。 My attempts have failed in F#. 我的尝试在F#中失败了。
Update: used interface now I just need to figure out serialization of the image channel object. 更新:现在使用的接口我只需要弄清楚图像通道对象的序列化。
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 =
Context Code: 上下文代码:
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
}
Should I implement the serialize interface directly? 我应该直接实现序列化接口吗? How do I override the abstract value member with a different type? 如何用其他类型覆盖抽象值成员? Any other suggestions? 还有其他建议吗?
I think you don't want brackets in your interface definition. 我认为您不希望在接口定义中使用方括号。
type Iobject() =
Should be 应该
type Iobject =
I have modified the Hello World sample http://tinyurl.com/pgxs6nv using interfaces and the serialization works correctly. 我已经使用接口修改了Hello World示例http://tinyurl.com/pgxs6nv ,序列化工作正常。 Could you give us more info about the error? 您能给我们更多有关该错误的信息吗?
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.