How to express asynchronous closure type?

I've an entry like:

rialight::initialize!(async move |_app| {
    // .await somewhere

I'm getting:

mismatched types
expected unit type ()
found opaque type impl Future<Output = ()>

I'm calling the closure after this line:

let user_ie: fn(Arc<Application>) -> () = $lambda_exp;

I tried to express the user_ie's return type as impl ::std::future::Future<Output = ()>. Here's the experiment defining the closure: and here's the crate defining the rialight::initialize! macro:

If the cast is inside the macro, you could try simply having the compiler infer the return type:

let user_ie: fn(Arc<Application>) -> _ = $lambda_exp;

By the way, there are two problems:

  1. If the passed closure is async, you need to .await it in order for it to have any effect at all.
  2. Why are you coercing it to a fn() pointer in the first place? Couldn't you just call it directly?
1 Like

Limiting the user's closure to be of a specific type avoids issues with the macro, I believe. If the closure does anything wrong, the error can go at the macro instead of the closure... But I didn't know you could just infer the return type...

Any part of any structured type is allowed to be inferred.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.