You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 1582 Reporter: administrator Status: closed Resolution: not a bug Priority: normal Severity: minor Category: ~DO NOT USE (was: OCaml general)
I don't know if it's really a bug or a feature of initializer (the explanation
given in the manual is quite short), but it has a weird behavior when combined
with multiple inheritance, especially when the inheritance graph has a 'diamond'
shape, as in the following example:
class root =
object
val mutable n = 1
initializer print_string "Initialization, pass "; print_int n;
print_newline(); n <- n+1
method get_n () = n
end
class foo =
object
inherit root
end
class bar =
object
inherit root
end
class foobar =
object
inherit bar
inherit foo
val mutable n = 3
method get_my_n ()= n
end
let y = new foobar;;
let x = y#get_n ();;
let z = y#get_my_n ();;
the output is the following:
Initialization, pass 1
Initialization, pass 1
val x : int = 2
val z : int = 3
It seems like
initializer is called twice, and each call uses a different instance variable
'n', whose initial value is 1 and which gets incremented (get_n returns 2).
Moreover, while get_n uses the same instance variable as one of the two
initializer, get_my_n uses the correct value, '3' which neither of the
initializer call seems to take into account.
E tutto per oggi, a la prossima volta
Virgile
The text was updated successfully, but these errors were encountered:
I don't know if it's really a bug or a feature of initializer (the
explanation given in the manual is quite short), but it has a weird
behavior when combined with multiple inheritance, especially when
the inheritance graph has a 'diamond' shape.
[...]
It seems like initializer is called twice, and each call uses a
different instance variable 'n', whose initial value is 1 and which
gets incremented (get_n returns 2). Moreover, while get_n uses the
same instance variable as one of the two initializer, get_my_n uses
the correct value, '3' which neither of the initializer call seems
to take into account.
This is the intended semantics: multiple inheritance in ocaml does not
allow instance variable sharing. So when there is a diamond, you just
get the same instance variables twice, and the initializers are called
independently (as in C++ IIRC).
For this reason, I would strongly suggest avoiding diamonds in the
inheritance graph. A better way is probably to define mixin-like
entities, using virtual private methods to access instance variables.
A bit heavy, but this is the only workaround I see for this design.
Original bug ID: 1582
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Bug description
Full_Name: Virgile Prevosto
Version: 3.06
OS: Linux (RH 7.2)
Submission from: 132.227.83.215 (132.227.83.215)
I don't know if it's really a bug or a feature of initializer (the explanation
given in the manual is quite short), but it has a weird behavior when combined
with multiple inheritance, especially when the inheritance graph has a 'diamond'
shape, as in the following example:
class root =
object
val mutable n = 1
initializer print_string "Initialization, pass "; print_int n;
print_newline(); n <- n+1
method get_n () = n
end
class foo =
object
inherit root
end
class bar =
object
inherit root
end
class foobar =
object
inherit bar
inherit foo
val mutable n = 3
method get_my_n ()= n
end
let y = new foobar;;
let x = y#get_n ();;
let z = y#get_my_n ();;
the output is the following:
Initialization, pass 1
Initialization, pass 1
val x : int = 2
val z : int = 3
It seems like
initializer is called twice, and each call uses a different instance variable
'n', whose initial value is 1 and which gets incremented (get_n returns 2).
Moreover, while get_n uses the same instance variable as one of the two
initializer, get_my_n uses the correct value, '3' which neither of the
initializer call seems to take into account.
E tutto per oggi, a la prossima volta
Virgile
The text was updated successfully, but these errors were encountered: