cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Unnecesary `async`/`Future`?

Open busslina opened this issue 2 years ago • 3 comments

/// Constructs a new [SecretKey] from the bytes.
///
/// Throws [ArgumentError] if the argument length is not [secretKeyLength].
Future<SecretKey> newSecretKeyFromBytes(List<int> bytes) async {
  if (bytes.length != secretKeyLength) {
    throw ArgumentError('Invalid secret key length');
  }
  return SecretKeyData(
    Uint8List.fromList(bytes),
    overwriteWhenDestroyed: true, // We copied the bytes so overwriting is ok.
  );
}

busslina avatar Nov 02 '23 20:11 busslina

I think because of the parallel web-support which enforces usage as Future<>.

justkawal avatar Nov 04 '23 16:11 justkawal

You can use this design:

abstract interface class Interface {
  FutureOr<SecretKey> newSecretKeyFromBytes(List<int> bytes);
}

class ImplementationWeb extends Interface {
  @override
  Future<SecretKey> newSecretKeyFromBytes(List<int> bytes) =>
      throw ('Not implemented');
}

class ImplementationNative extends Interface {
  @override
  SecretKey newSecretKeyFromBytes(List<int> bytes) => throw ('Not implemented');
}

This async is make me changing too much method signatures

busslina avatar Nov 05 '23 18:11 busslina

I would have to agree with this. I want to use cryptography in the uuid library, but it being async, would require me to change my entire library to async functions, which would not only break everyone using the library, but is unnecessary.

The above design would go a long way to solving that for those that don't need it.

daegalus avatar Feb 08 '24 20:02 daegalus