Why is it possible to omit default values in overridden member functions of sub-types?

后端 未结 2 900
無奈伤痛
無奈伤痛 2021-02-02 09:44

Just as stated in the title: Why is it possible to omit default values in overridden member functions of sub-types?

Is this normal or to be expected?

         


        
2条回答
  •  执笔经年
    2021-02-02 09:55

    Yes, this is normal and to be expected.

    Moreover, it is not allowed to override the default values. This does not compile:

    interface Foo {
        fun bar(parameter: Int = 1)
    }
    
    class Baz : Foo {
        override fun bar(parameter: Int = 1) { // ERROR: An overriding function is not allowed to specify default values for its parameters
        }
    }
    

    Technically the implementation with fixed default values is much simpler than any other implementation. For example Scala goes to a great extend to provide this capability, but the generated byte code is not a simple one.

提交回复
热议问题