Menu

#4265 Surprising behaviour of empty constructor

obsolete: 8.6b1
closed-invalid
5
2009-02-15
2009-01-23
No

I found the behaviour exhibited in the wish session below surprising. Defining an empty constructor behaves as though no constructor was defined for the class at all. In particular,
- I would expect the derived class constructor to "hide" the superclass constructor and not execute it since there is no next call. Creating a D object is seen to run the C construtor
- I would expect info class constructor D to return a pair containing the arg a and an empty body
- A constructor defined with an empty body behaves the same as a constructor defined with the statement "next" (or similar, there might be differences in presence mixins). That does not seem right
- Adding a comment to a body changes the code that is executed - see the difference between behaviour of class D and E, the only difference being addition of a comment. This to me is the biggest NO-NO.

(web) 2 % oo::class create C {
> constructor {a} {set ::x $a}
> }
::C
(web) 3 % oo::class create D {
> superclass C
> constructor {a} {}
> }
::D
(web) 4 % info class constructor D
(web) 5 % info class constructor C
a {set ::x $a}
(web) 6 % D create d
wrong # args: should be "D create d a"
(web) 7 % D create d 1
::d
(web) 8 % set x
1
(web) 10 % oo::class create E {
> superclass C
> constructor {a} { # Just a comment }
> }
::E
(web) 11 % set x 0
0
(web) 12 % E create e
wrong # args: should be "E create e a"
(web) 13 % E create e abc
::e
(web) 14 % set x
0

/Ashok

Discussion

  • Donal K. Fellows

    If the body of a constructor (or destructor) is the empty string, the constructor(/destructor) is deleted. This is documented.

     
  • Donal K. Fellows

    • status: open --> closed-invalid