whenOrNull<TResult extends Object?> method

  1. @optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  1. TResult? $default(
    1. XrayAddress? address,
    2. int? port,
    3. TransportProtocol? method,
    4. TransportProtocol? network,
    5. SecurityProtocol? security,
    6. FinalMask? finalmask,
    7. TCPConfig? rawSettings,
    8. TCPConfig? tcpSettings,
    9. SplitHTTPConfig? xhttpSettings,
    10. KCPConfig? kcpSettings,
    11. WebSocketConfig? wsSettings,
    12. HttpUpgradeConfig? httpupgradeSettings,
    13. SplitHTTPConfig? splithttpSettings,
    14. GRPCConfig? grpcSettings,
    15. HysteriaConfig? hysteriaSettings,
    16. TLSConfig? tlsSettings,
    17. REALITYConfig? realitySettings,
    18. SocketConfig? sockopt,
    )?
)

A variant of when that fallback to returning null

It is equivalent to doing:

switch (sealedClass) {
  case Subclass(:final field):
    return ...;
  case _:
    return null;
}

Implementation

@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
  TResult? Function(
          XrayAddress? address,
          int? port,
          TransportProtocol? method,
          TransportProtocol? network,
          SecurityProtocol? security,
          FinalMask? finalmask,
          TCPConfig? rawSettings,
          TCPConfig? tcpSettings,
          SplitHTTPConfig? xhttpSettings,
          KCPConfig? kcpSettings,
          WebSocketConfig? wsSettings,
          HttpUpgradeConfig? httpupgradeSettings,
          SplitHTTPConfig? splithttpSettings,
          GRPCConfig? grpcSettings,
          HysteriaConfig? hysteriaSettings,
          TLSConfig? tlsSettings,
          REALITYConfig? realitySettings,
          SocketConfig? sockopt)?
      $default,
) {
  final _that = this;
  switch (_that) {
    case _StreamConfig() when $default != null:
      return $default(
          _that.address,
          _that.port,
          _that.method,
          _that.network,
          _that.security,
          _that.finalmask,
          _that.rawSettings,
          _that.tcpSettings,
          _that.xhttpSettings,
          _that.kcpSettings,
          _that.wsSettings,
          _that.httpupgradeSettings,
          _that.splithttpSettings,
          _that.grpcSettings,
          _that.hysteriaSettings,
          _that.tlsSettings,
          _that.realitySettings,
          _that.sockopt);
    case _:
      return null;
  }
}