Java API上的scala API包装器

Java API上的scala API包装器,java,scala,interface,future,Java,Scala,Interface,Future,我是Scala新手,正在为我的JavaAPI构建Scala包装器 我有四个Java接口 public interface Client<T> { <T> Handle<T> execute(App<T> app); } public interface App<T> extends Serializable{ T process(AppContext context) throws Exception; } public in

我是Scala新手,正在为我的JavaAPI构建Scala包装器

我有四个Java接口

public interface Client<T> {
 <T> Handle<T> execute(App<T> app);
}

public interface App<T> extends Serializable{
  T process(AppContext context) throws Exception;
}

public interface AppContext {
  File getDirectory();
  void deleteDirectory();
  File createDirectory(String path);
}

public interface Handle<T> extends Future<T> {
   static interface Listener<T> {          
       void onSuccess(Handle<T> handle)
       void onFailure(Handle<T> handle, Throwable e)
   }

   void addListener(Listener<T> listener);
}
我得到以下错误

匿名函数的参数类型必须完全已知。(补充说明8.5)

预期类型为:单位 重写def onComplete[U](func:(Try[T])=>U)(隐式执行器:ExecutionContext)={


另外,我想知道我的方法是否正确

您的问题不在于
onComplete
本身,而在于您实现它的方式

override def onComplete[U](func: (Try[T]) => U)(implicit executor: ExecutionContext)  = {
  case Success(t) => promise.success(test.get())
  case Failure(e) => promise.failure(e)
}
在这里,您将
onComplete
定义为某个函数的部分函数
Try[\U]=>U
-类型未知,并且它肯定不会覆盖原始方法。请注意,
case
与此处的任何内容都不匹配:这里的
func
不是
Try[T]

作为旁注,我不得不提到,你永远不应该扩展
未来的
。一个非常有用的座右铭是,不惜一切代价避免继承。在你的情况下,我认为继承不会为你赢得任何东西,相反,它会增加混乱

你可能要找的是

def asFuture[T](handle: Handle[T]): Future[T] = {
  val promise = Promise[T]()
  handle.addListener(new Handle.Listener[T] {
    def onSuccess(handle: Handle[T]) {
      promise.trySuccess(handle.get())
    }
    def onFailure(handle: Handle[T], e: Throwable) {
      promise.tryFailure(e)
    }
  })
  promise.future
}

您的问题不在于
onComplete
本身,而在于您实现它的方式

override def onComplete[U](func: (Try[T]) => U)(implicit executor: ExecutionContext)  = {
  case Success(t) => promise.success(test.get())
  case Failure(e) => promise.failure(e)
}
在这里,您将
onComplete
定义为某个函数的部分函数
Try[\U]=>U
-类型未知,并且它肯定不会覆盖原始方法。请注意,
case
与此处的任何内容都不匹配:这里的
func
不是
Try[T]

作为旁注,我不得不提到,你永远不应该扩展
未来的
。一个非常有用的座右铭是,不惜一切代价避免继承。在你的情况下,我认为继承不会为你赢得任何东西,相反,它会增加混乱

你可能要找的是

def asFuture[T](handle: Handle[T]): Future[T] = {
  val promise = Promise[T]()
  handle.addListener(new Handle.Listener[T] {
    def onSuccess(handle: Handle[T]) {
      promise.trySuccess(handle.get())
    }
    def onFailure(handle: Handle[T], e: Throwable) {
      promise.tryFailure(e)
    }
  })
  promise.future
}

很抱歉没有提供清楚的信息。我已经编辑了我的问题。我需要在这里继承,以便为我的API提供一个无阻塞的scala未来。你不需要,你只需要将其视为一种合理的方式。我建议你编写一个函数,该函数采用
句柄,
在其上注册一个侦听器,并且当底层
句柄
是完整的,完成了一个
承诺
。现在您在这里返回的唯一一件事是
Promice
。未来
-没有继承,没有耦合。但是在onComplete中使用它将是一个理想的Scala API。虽然可以通过添加侦听器来实现,但我觉得这不是Scala实现的方式;
onComplete
已经由
Future
提供,不需要实现新的。在您发布的上述代码中,它应该是
promise.trySucces(handle.get)
?因为
Handle扩展了JFuture
很抱歉没有提供明确的信息。我已经编辑了我的问题。我需要在这里继承,以便为我的API提供一个非阻塞的scala future。你不需要这样做,你只需将其视为一种合理的方式。我建议你编写一个函数,它采用
Handle,
注册一个当底层的
句柄完成时,它就完成了一个
承诺
。现在您在这里返回的唯一内容是
Promice
。future
-没有继承,没有耦合。但是在onComplete中使用它将是一个理想的Scala API。虽然可以通过添加侦听器来实现,但我觉得它不是e scala这样做的方式是不会的;
onComplete
已经由
Future
提供了,没有必要实现新的。在您发布的上述代码中,它应该是
promise.trySuccess(handle.get)
?因为
handle扩展了JFuture