I probably should've suggested this instead, instead of that needlessly complex thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
type Manager (filters: Map<string, string>) =
  member this.Filters = filters
  static member private lock = new obj()
  static member private active: Manager option ref = ref None
  static member GetActiveManager = 
      lock (lock) ( 
        fun () -> 
          match !Manager.active with 
          | Some v -> v 
          | None -> 
          let m = new Manager(Map<string, string>([]))
          Manager.active := Some m
          m
      )

I've made active private, I assumed the GetActiveManager method should encapsulate it. An advantage of this approach.Also consider the code given in the original thread to abstract out the singleton related code.Edit: If you prefer lets:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Manager (filters: Map<string, string>) =
  static let active: Manager option ref = ref None
  static let lockMgr = new obj()
  member this.Filters = filters
  static member GetActiveManager = 
      lock (lockMgr) ( 
        fun () -> 
          match !active with 
          | Some v -> v 
          | None -> 
          let m = new Manager(Map<string, string>([]))
          active := Some m
          m
      )
By on 3/29/2009 12:37 PM ()

You are absolutely right, encapsulating it all inside the manager class was the intention from the start. With your help I ended with the following:

1
2
3
4
5
6
7
type Manager (filters: Map<string, string>) =
  static let active = ref new Manager(Map<string, string>([]))
  static let lockMgr = new obj()
  member this.Filters = filters
  static member GetActiveManager = 
  lock (lockMgr) (fun () -> !active)
.....

BTW, the difference between the lets and members seems to be more than a matter preference: when I tried to run it with members, it was executing the obj() every time the lockMgr was requested, effectively defeating the locks

By on 3/29/2009 3:04 PM ()

BTW, the difference between the lets and members seems to be more than a matter preference: when I tried to run it with members, it was executing the obj() every time the lockMgr was requested, effectively defeating the locks

Right. Can't believe I fell for that one _again_ :) The reason is that static members are actually properties, while static lets are fields...In C# you would have:

1
static object LockMgr{  get  {    return new object();  }}

which makes it more obvious what's going on.

By on 3/30/2009 12:36 AM ()
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