grpc-dart icon indicating copy to clipboard operation
grpc-dart copied to clipboard

Use FutureOr instead of Future for ServiceBase

Open ghost opened this issue 3 years ago • 1 comments

It will be more convinece to use the FutureOr from dart:async, since it makes more easy to implement simple method without adding the word async infront every one.

instead of:

class GreeterService extends GreeterServiceBase {
  @override
  Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
    return HelloReply()..message = 'Hello, ${request.name}!';
  }
}

do this:

class GreeterService extends GreeterServiceBase {
  @override
  FutureOr<HelloReply> sayHello(ServiceCall call, HelloRequest request) {
    return HelloReply()..message = 'Hello, ${request.name}!';
  }
}

ghost avatar May 23 '22 16:05 ghost

FutureOr is hard to work with on the user's side. When you receive a FutureOr, you don't know whether you have to await or if you can use it directly. It's like using Object or dynamic, in that it tells you nothing about the type -- "it could be a future, or it could be a regular object" is not really helpful. It's much simpler to get a Future and await it every time, and that simplicity is probably worth typing out the async

Levi-Lesches avatar Sep 09 '22 07:09 Levi-Lesches