11

I am writing an F# type, and I'm having trouble figuring out how to reference a member function from the constructor upon initialization. I think I'm supposed to use a do binding, but then the do binding can't understand the member functions. Is there no way around this?

user3685285
  • 6,066
  • 13
  • 54
  • 95

1 Answers1

16

You can do it this way:

type MyClass() as this =   // Note as this

    do this.SayHello()

    member this.SayHello() = 
        do printfn "Hello from constructor!"

But generally it is not a good practice

Petr
  • 4,280
  • 1
  • 19
  • 15
  • 1
    Thanks. It works as you promised, but why is it not good practice? – user3685285 Mar 23 '15 at 14:38
  • 10
    Let say that method uses other class fields or members that not yet initialized or depend on some extra resource (file, connection etc.) that could be not ready. In this case you'll get runtime exception at construction. If you take into consideration inheritance things may become even worse. – Petr Mar 23 '15 at 14:46