1
2
3
4
5
6
7
type Foo private() = 
    member x.F() = ()
    static member Create() = new Foo()

let f1 = new Foo()  // does not compile
let f2 = Foo.Create()
f2.F()

Note the location of the 'private' keyword.

By on 11/10/2008 6:40 PM ()

Ahh, interesting :-) thanks for the correction!

T.

By on 11/10/2008 7:20 PM ()

Cool, thanks Brian and Tomas!

Kurt

By on 11/11/2008 1:22 AM ()

Hi Kurt,
this is an interesting problem! I think you can't really do that.
You can write the following code:

1
2
3
4
5
 
type private Bar() =
  do printfn "bar"
  static member Create() = 
    new Bar()

However, this marks both the class and the constructor private, so you can't use the class outside of the assembly. It seems that there is no way to change its visibility "back" to public (e.g. using fsi (signature) file). You can use signature file to make the constructor inaccessible from outside of the file where it is defined, but it'll still remain accessibe from the implementation file:

1
2
3
4
5
6
7
8
9
10
11
 
// test.fsi
[<Class>]
type Bar =
  static member Create : unit -> Bar

// test.fs
type Bar() =
  do printfn "bar"
  static member Create() = 
    new Bar()

But maybe I'm missing something here... I'll forward this question to the F# team to see if there are some suggestions how to deal with this!

T.

By on 11/10/2008 5:28 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