I am writing a little machine learning engine on top of the orleans framework with orleankka. I need a parent child class relationship where the parent supports get, set, default constructor and serialization. My attempts have failed in 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. 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"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.