Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# object refer to self in let bindings before members

Tags:

f#

Is it possible for an object to refer to itself before member definitions? Something like this:

type public ClassA () = class
    let b = new ClassB(this)
    member public x.getClassB() = b
    end

where 'this' is a reference to the instance of ClassA creating the ClassB?

like image 959
user3685285 Avatar asked Jun 29 '15 21:06

user3685285


1 Answers

Classes can refer to themselves in their constructors using as:

type public ClassA () as this = 
    let b = new ClassB(this)
    member public x.getClassB() = b
and public ClassB (a: ClassA) =
    member public x.getClassA() = a
like image 161
Lee Avatar answered Sep 22 '22 23:09

Lee