when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. bool? noKernelTun,
    2. String secretKey,
    3. List<String>? address,
    4. List<WireGuardPeerConfig>? peers,
    5. @JsonKey.new(name: 'mtu') int? mtu,
    6. @JsonKey.new(name: 'workers') int? numWorkers,
    7. List<int>? reserved,
    8. XrayTargetStrategy? domainStrategy,
    )
)

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(
          bool? noKernelTun,
          String secretKey,
          List<String>? address,
          List<WireGuardPeerConfig>? peers,
          @JsonKey(name: 'mtu') int? mtu,
          @JsonKey(name: 'workers') int? numWorkers,
          List<int>? reserved,
          XrayTargetStrategy? domainStrategy)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _WireGuardConfig():
      return $default(
          _that.noKernelTun,
          _that.secretKey,
          _that.address,
          _that.peers,
          _that.mtu,
          _that.numWorkers,
          _that.reserved,
          _that.domainStrategy);
    case _:
      throw StateError('Unexpected subclass');
  }
}