Hmm, your code lost the angle brackets when it was posted, see

[link:cs.hubfs.net]

Anyway, I tried to fix it up; the code below compiles, though I have not tried to reason through what you are doing at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 

module Statefulie
  /// A type that maintains an internal state.
  type IStateful<'a> =
    abstract member Status : 'a with get, set
  
  /// A state transition record.
  type Transition<'a> = {
    Target : 'a IStateful
    Event  : 'a IEvent
    From   : 'a
    To     : 'a
  }

  /// A state machine record.
  type StateMachine<'a> = {
    Target      : 'a IStateful
    Transitions : 'a Transition list
  }

  let createTarget (state: 'a) =
    let status = Some(state) |> ref
    { new IStateful<'a option> with
        member this.Status with get() = !status
                           and  set s = status := s
    }

  let exiting<'a> = new Event<_>()
  let onExit<'a> = exiting.Publish
  let entering<'a> = new Event<_>()
  let onEntry<'a> = entering.Publish

  let on event target =
    { Target = target; Event = event; From = None; To = None }

  let from state t =
    let status = Some(state)
    let fromEvent = t.Event |> Event.filter (fun s -> s = status)
    { t with Event = fromEvent; From = status }

  let into state t =
    let status = Some(state)
    let cancel = t.Event.Subscribe (fun s -> exiting.Trigger(s)
                                             entering.Trigger(status)
                                             t.Target.Status <- status)
    { t with To = status }
 

By on 2/14/2010 4:49 PM ()

Just from the the definition, it seems that IStateful<'a> must have 'a where 'a must contains get,set. But option type doesn't have it(inferred to be 'a in your record). Which I believe is where the inconsistency is coming from. Would the following be what you really want ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 

type Transition = {

    Target : 'a IStateful

    Event  : 'a IEvent

    From   : 'a option

    To     : 'a option

  }

By on 2/14/2010 3:51 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper