Should empty parentheses for super-constructors be required?


#1

I’m wondering if the () must be required in the second line of this example:

open class Base
class Derived : Base()

I can declare the base class without brackets, it would be nice if the same were true for the inheritance relationship.


#2

The parentheses indicate which of the base types of your class is a class (requiring parentheses) and which is an interface (where no parentheses are allowed).


#3

Sorry, should have given more context.

I do know about this distinction. BUT:

  • It is impossible to instantiate interfaces, thus I can never call the constructor for them anyway. If I really want to know more details about the base I can always press CTRL+J to show quick java doc or CTRL+H to show hierarchy.
  • I believe I read somewhere that one of the reasons for the Derived : Base syntax was to do away with the distinction between extends and implements that you have in Java. I feel like requiring brackets sneaks this distinction back in

I would find it more consistent if I could get rid of the brackets (or always require them class declarations). Just a thought :slight_smile:


#4

I’d like to see the () removed too, it’s just extra noise (make it optional so it doesn’t break anything).


#5

The trade-off here is

  • extra parentheses, versus
  • knowledge of which supertype if a class.

Not so easy to tell which is better, really, so this cleasly goes under the “bikeshedding” category :slight_smile:


#6

If you think of a constructor call, this syntax makes sense. A constructor is more or less a function and you have to write parenthesis when calling parameterless functions, too.


#7

For completeness, this variant should be included in the discussion:

open class Base

class Derived : Base {
    constructor() : super()
}