when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String name,
    2. bool isDeprecated,
    3. bool isExperimental,
    4. bool isSealed,
    5. bool isAbstract,
    6. Set<TypeUsage> typeUsages,
    7. List<String> typeParameterNames,
    8. Set<String> superTypeNames,
    9. List<ExecutableDeclaration> executableDeclarations,
    10. List<FieldDeclaration> fieldDeclarations,
    11. Set<String>? entryPoints,
    12. String relativePath,
    )
)

A switch-like method, using callbacks.

As opposed to map, this offers destructuring. It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case Subclass2(:final field2):
    return ...;
}

Implementation

@optionalTypeArgs
TResult when<TResult extends Object?>(
  TResult Function(
          String name,
          bool isDeprecated,
          bool isExperimental,
          bool isSealed,
          bool isAbstract,
          Set<TypeUsage> typeUsages,
          List<String> typeParameterNames,
          Set<String> superTypeNames,
          List<ExecutableDeclaration> executableDeclarations,
          List<FieldDeclaration> fieldDeclarations,
          Set<String>? entryPoints,
          String relativePath)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _InterfaceDeclaration():
      return $default(
          _that.name,
          _that.isDeprecated,
          _that.isExperimental,
          _that.isSealed,
          _that.isAbstract,
          _that.typeUsages,
          _that.typeParameterNames,
          _that.superTypeNames,
          _that.executableDeclarations,
          _that.fieldDeclarations,
          _that.entryPoints,
          _that.relativePath);
  }
}