Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke methods from constructor in F#

Tags:

constructor

f#

I'm aware of this question, but the asker seems to have been content with an answer to another question (how to overload the constructor)

I have a class which kind of acts as an advanced memoizer around a mutable class, so that I can treat it as immutable from outside:

type Wrapper(args) =
    let tool = new MutableTool()
    tool.Init(args)  //<--"Unexpected identifier in definition"

    let lookupTable = //create lookup using tool here
    member this.Lookup(s) = //callers use lookupTable here

I can't work out how to invoke the Init method on "tool". What am I missing?

like image 892
Benjol Avatar asked Oct 26 '09 12:10

Benjol


2 Answers

IIRC, the do keyword might work here:

type Wrapper(args) =
    let tool = new MutableTool()
    do tool.Init(args)

    let lookupTable = //create lookup using tool here
    member this.Lookup(s) = //callers use lookupTable here

I'm not sure what you meant with the last line of code, so I left it as you wrote it...

like image 167
Mark Seemann Avatar answered Oct 20 '22 01:10

Mark Seemann


You need "do":

type Foo(args) = 
  let x = new Whatever()
  do x.Bar()

  member ....
like image 33
MichaelGG Avatar answered Oct 20 '22 00:10

MichaelGG