Skip to content

[Feature] Not require a default value when arguments are given the default value from it's super class #2423

Closed as not planned
@AlexV525

Description

@AlexV525

I didn't find any related issues, please link to this if any.

Consider the code:

class A {
  void test({
    String test = 'default',
  }) {}
}

class C extends A {
  @override
  void test({
    String test, // <- missing_default_value_for_parameter
  }) {}
}

The above code will always report an error in the null-safety world (not sure if it's the same in non-null-safety). To avoid the error, we might turn the code into this to avoid redundant hard-code content:

const _defaultTestString = 'default';

class A {
  void test({
    String test = _defaultTestString,
  }) {}
}

class C extends A {
  @override
  void test({
    String test = _defaultTestString,
  }) {}
}

However, in Kotlin, the default value cannot be re-defined if it's overriding a method:

open class A() {
    open fun test(test: String = "default") {}
}

class C() : A() {
    override fun test(
        test: String = "default" // <- Error: An overriding function is not allowed to specify default values for its parameters
    ) {}
}

I think it can be useful for Dart to handle this, or we can make arguments as super arguments (but super here seems in a bad semantic condition):

class A {
  void test({String test = 'default'}) {}
}

class C extends A {
  @override
  void test({super.test}) {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions