Access to objects under construction
Basically I cannot access objects under construction as the following codes demonstrates.
type c1 = class
val x1:int
new (i) as this = { x1 = i } then this.f ()
abstract f : unit -> unit
end;;
type c3 = class
inherit c1
val x2: int
val x3: int
new (i) as this =
{ inherit c1 (i);
x2 = i+1;
x3 = this.x2
}
override self.f () = ()
end;;
let x2 = new c3 1;;
System.NullReferenceException
But in fact I can. (Note that the following code only type check with fsc, but not with fsi.)
type c1 = class
val x1:int
new (i) as this = { x1 = i } then this.f ()
abstract f : unit -> unit
end;;
let pool = ref None;;
type c2 = class
inherit c1
val x2: int
val x3: int
new (i) =
{ inherit c1 (i);
x2 = i+1;
x3 = let (base: c2) = match !pool with None -> failwith "error" | Some base -> base in
(base.x2 + 1)
}
override self.f () = pool := (Some self)
end;;
let x = new c2 (0);;
print_int x.x1;;
print_int x.x2;;
print_int x.x3;;
012
Implicit class construction
If implicit class construction adds the possibilities of
observable null's, it costs too much.
> type t = A of int;;
> type a (x:t) = class
new () = {}
member self.get = x
end;;
> let x = new a ();;
> x.get;;
val it : t = null
> let i = match x.get with A i -> i;;
System.NullReferenceException
First, there would be no way to correct x.get's behavior afterward.
Second, I am not familiar with the fact that datatypes (or discriminated union types)
have the default value null.