Skip to content

Nested class declaration #56625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
feduke-nukem opened this issue Sep 2, 2024 · 2 comments
Closed

Nested class declaration #56625

feduke-nukem opened this issue Sep 2, 2024 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug

Comments

@feduke-nukem
Copy link

Proposal: Opportunity to declare classes inside other classes like in Kotlin:

sealed class SomeState<T>{
    class Loading<T> : SomeState<T>()

    data class Success<T>(val value:T) : SomeState<T>()

    data class Error<T>(val error: Throwable) : SomeState<T>()
}

fun handle(state: SomeState<Int>){
    when(state){
        is SomeState.Error -> TODO()
        is SomeState.Loading -> TODO()
        is SomeState.Success -> TODO()
    }
}

Dart:

sealed class SomeState<T> {
  class Loading<T> extends SomeState<T> {}

  class Success<T> extends SomeState<T> {
    final T value;

    Success(this.value);
  }

  class Error<T> extends SomeState<T> {
    final Object error;

    Error(this.error);
  }
}

void handle(SomeState<int> state) {
  switch (state) {
    case SomeState.Loading():
    case SomeState.Success():
    case SomeState.Error():
  }
}

Use case: to increase readability especially for sealed classes

Without such syntax we should always add "noisy" prefix/suffix to sealed subclasses' names especially when super class has long name:

sealed class SomeVeryVeryVeryLongNamedState<T> {}

class SomeVeryVeryVeryLongNamedStateLoading<T>
    extends SomeVeryVeryVeryLongNamedState<T> {}

class SomeVeryVeryVeryLongNamedStateSuccess<T>
    extends SomeVeryVeryVeryLongNamedState<T> {
  final T value;

  SomeVeryVeryVeryLongNamedStateSuccess(this.value);
}

class SomeVeryVeryVeryLongNamedStateError<T>
    extends SomeVeryVeryVeryLongNamedState<T> {
  final Object error;

  SomeVeryVeryVeryLongNamedStateError(this.error);
}

void handle(SomeVeryVeryVeryLongNamedState<int> state) {
  switch (state) {
    case SomeVeryVeryVeryLongNamedStateLoading():
    case SomeVeryVeryVeryLongNamedStateSuccess():
    case SomeVeryVeryVeryLongNamedStateError():
  }
}
@dart-github-bot
Copy link
Collaborator

Summary: This issue proposes adding support for nested class declarations in Dart, similar to Kotlin. The goal is to improve readability, especially for sealed classes, by eliminating the need for verbose prefixes or suffixes in subclass names.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Sep 2, 2024
@julemand101
Copy link
Contributor

This is not a Dart SDK issue but instead an issue for the Dart Specification issue tracker which can be found at: https://github.com/dart-lang/language/issues

Your issue looks like a duplicate of: dart-lang/language#336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants