Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kotlin

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?

interface Foo {   fun bar(parameter: Int = 1) }  class Baz : Foo {   override fun bar(parameter: Int) { // OK     println(parameter)   } }  val baz = Baz()  baz.bar() // OK baz.bar(2) // OK 

Same behavior in the case where Foo is a class.

like image 372
EPadronU Avatar asked Jun 08 '16 11:06

EPadronU


People also ask

When overriding methods with default parameter values you must specify a new default value for the parameters?

A default value is defined using = after the type. Overriding methods always use the same default parameter values as the base method. When overriding a method that has default parameter values, the default parameter values must be omitted from the signature: open class A { open fun foo(i: Int = 10) { /*...

What will happen if a virtual function has any default value parameter?

Default Arguments and Virtual Function in C++ In case any value is passed the default value is overridden and it becomes a parameterized argument. Virtual function is a member function that is declared within a base class and is redefined(Overridden) by a derived class.

How do I change the default value in Kotlin?

Kotlin Default Argument In Kotlin, you can provide default values to parameters in function definition. If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default arguments are used.

How do you pass arguments in Kotlin?

In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.


2 Answers

Is this normal or to be expected?

Yes.

I suspect this was primarily a language design / usability decision. From this perspective, there were four options available:

  1. Declare in supertype only.
  2. Declare in subtype only.
  3. Declare in both, but do not allow subtype to change the default.
  4. Declare in both, allow subtype to override the default in the supertype.

The Kotlin designers chose option #1. This makes sense because:

Option #2 and #4 all imply that callers would not know what the default value is unless they were aware of which implementation they were using, which is of course highly undesirable. The caller would require extra logic to determine if a value is needed to override a default, which means the default would be useless.

Option #3 violates the DRY principle. Why force the declaration to be in two places?

That leaves Option #1 as the only sane choice.

like image 60
Raman Avatar answered Oct 06 '22 04:10

Raman


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.

like image 26
voddan Avatar answered Oct 06 '22 03:10

voddan