when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. String protocol,
    2. XrayPortList? port,
    3. XrayAddress? listen,
    4. XrayInboundSettings? settings,
    5. String? tag,
    6. StreamConfig? streamSettings,
    7. SniffingConfig? sniffing,
    )
)

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 protocol,
          XrayPortList? port,
          XrayAddress? listen,
          XrayInboundSettings? settings,
          String? tag,
          StreamConfig? streamSettings,
          SniffingConfig? sniffing)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _InboundDetourConfig():
      return $default(_that.protocol, _that.port, _that.listen,
          _that.settings, _that.tag, _that.streamSettings, _that.sniffing);
    case _:
      throw StateError('Unexpected subclass');
  }
}