dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
105 stars 36 forks source link

Generating async functions for Java #552

Closed Gaurav-CareMonitor closed 9 months ago

Gaurav-CareMonitor commented 9 months ago

The function getting generated are not Futures even tho it is supposed to be.

image

This function should be awaited.

Code: here

Here is the yaml file:


# Generating from gradle dependencies
android_sdk_config:
  add_gradle_deps: true

summarizer:
  # Instead of Java source code, we want to generate from classes
  backend: asm

suspend_fun_to_async: true

output:
  c:
    library_name: health_connect
    path: src/health_connect/
  dart:
    path: lib/health_connect.dart
    structure: single_file

enable_experiment:
  - interface_implementation
HosseinYousefi commented 9 months ago

This is expected behavior. The function sdkAuthWithLicense has a return type of a boolean so the generated function is also returning bool and not Future<bool>.

Gaurav-CareMonitor commented 9 months ago

So is there no way of getting data from Future Java functions? @HosseinYousefi

HosseinYousefi commented 9 months ago

The function you mention is not even a Future it seems. There are many different ways of creating asynchronous methods in other langauges. For example, in android, you might get a Task<T> back. In this case, the correct way to consume the result of this "task" is to addOnCompleteListener and such methods. Java also have a Future class. Kotlin uses suspendable functions. A library creator can create an arbitrary class similar to Task that can act async. Etc.

Therefore, it's not really possible to account for ALL of these different constructs and convert them all into Futures (to enable async/await) in Dart. You can still add the callbacks for classes such as Task (similar to .then in Futures in Dart) and use them.

There is a direct translation from Kotlin's suspend fun to Dart's async method that jnigen supports. For all the other constructions, you would have to use some form of callback.