when<TResult extends Object?> method

  1. @optionalTypeArgs
TResult when<TResult extends Object?>(
  1. TResult $default(
    1. bool? allowInsecure,
    2. @JsonKey.new(name: 'certificates') List<TLSCertConfig>? certs,
    3. String? serverName,
    4. @JsonKey.new(name: 'alpn') XrayStringList? alpn,
    5. bool? enableSessionResumption,
    6. bool? disableSystemRoot,
    7. String? minVersion,
    8. String? maxVersion,
    9. String? cipherSuites,
    10. String? fingerprint,
    11. @JsonKey.new(name: 'rejectUnknownSni') bool? rejectUnknownSNI,
    12. XrayStringList? curvePreferences,
    13. String? masterKeyLog,
    14. String? pinnedPeerCertSha256,
    15. String? verifyPeerCertByName,
    16. List<String>? verifyPeerCertInNames,
    17. String? echServerKeys,
    18. String? echConfigList,
    19. ECHForceQuery? echForceQuery,
    20. @JsonKey.new(name: 'echSockopt') SocketConfig? echSocketSettings,
    )
)

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? allowInsecure,
          @JsonKey(name: 'certificates') List<TLSCertConfig>? certs,
          String? serverName,
          @JsonKey(name: 'alpn') XrayStringList? alpn,
          bool? enableSessionResumption,
          bool? disableSystemRoot,
          String? minVersion,
          String? maxVersion,
          String? cipherSuites,
          String? fingerprint,
          @JsonKey(name: 'rejectUnknownSni') bool? rejectUnknownSNI,
          XrayStringList? curvePreferences,
          String? masterKeyLog,
          String? pinnedPeerCertSha256,
          String? verifyPeerCertByName,
          List<String>? verifyPeerCertInNames,
          String? echServerKeys,
          String? echConfigList,
          ECHForceQuery? echForceQuery,
          @JsonKey(name: 'echSockopt') SocketConfig? echSocketSettings)
      $default,
) {
  final _that = this;
  switch (_that) {
    case _TLSConfig():
      return $default(
          _that.allowInsecure,
          _that.certs,
          _that.serverName,
          _that.alpn,
          _that.enableSessionResumption,
          _that.disableSystemRoot,
          _that.minVersion,
          _that.maxVersion,
          _that.cipherSuites,
          _that.fingerprint,
          _that.rejectUnknownSNI,
          _that.curvePreferences,
          _that.masterKeyLog,
          _that.pinnedPeerCertSha256,
          _that.verifyPeerCertByName,
          _that.verifyPeerCertInNames,
          _that.echServerKeys,
          _that.echConfigList,
          _that.echForceQuery,
          _that.echSocketSettings);
    case _:
      throw StateError('Unexpected subclass');
  }
}