Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initializer and multiple inheritance #8053

Closed
vicuna opened this issue Mar 10, 2003 · 2 comments
Closed

initializer and multiple inheritance #8053

vicuna opened this issue Mar 10, 2003 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Mar 10, 2003

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

@vicuna
Copy link
Author

vicuna commented Mar 15, 2003

Comment author: administrator

Bad design? (Jacques)

@vicuna vicuna closed this as completed Mar 15, 2003
@vicuna
Copy link
Author

vicuna commented Mar 15, 2003

Comment author: administrator

From: virgile.prevosto@lip6.fr

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.

Jacques

@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant