简体   繁体   English

Scala RxJava参数表达式的类型与形式参数类型不兼容

[英]Scala RxJava argument expression's type is not compatible with formal parameter type

I was trying to implement simple websocket handling using Vertx toolchain using RxJava library and Scala language. 我试图使用RxJava库和Scala语言使用Vertx工具链实现简单的Websocket处理。

And I'm getting error passing anonymous class to RxJava map method 我在将匿名类传递给RxJava map方法时遇到错误

websocket
    .flatMap(socket => socket.toObservable)
    .map(new Function[Buffer, String] {
      override def apply(msg: Buffer): String = {
        msg.toString
      }
    })

Compiler stack trace: 编译器堆栈跟踪:

Error:(61, 6) no type parameters for method map: (x$1: io.reactivex.functions.Function[_ >: io.vertx.reactivex.core.buffer.Buffer, _ <: R])io.reactivex.Observable[R] exist so that it can be applied to arguments (io.reactivex.functions.Function[io.vertx.reactivex.core.buffer.Buffer,String])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : io.reactivex.functions.Function[io.vertx.reactivex.core.buffer.Buffer,String]
 required: io.reactivex.functions.Function[_ >: io.vertx.reactivex.core.buffer.Buffer, _ <: ?R]
Note: io.vertx.reactivex.core.buffer.Buffer <: Any, but Java-defined trait Function is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
    .map(new Function[Buffer, String] {


Error:(61, 10) type mismatch;
 found   : io.reactivex.functions.Function[io.vertx.reactivex.core.buffer.Buffer,String]
 required: io.reactivex.functions.Function[_ >: io.vertx.reactivex.core.buffer.Buffer, _ <: R]
    .map(new Function[Buffer, String] {

map method signature in RxJava: RxJava中的map方法签名:

@CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    public final <R> Observable<R> map(Function<? super T, ? extends R> mapper) {
        ObjectHelper.requireNonNull(mapper, "mapper is null");
        return RxJavaPlugins.onAssembly(new ObservableMap<T, R>(this, mapper));
    }

In compiler stack trace I see that function receives lower bound Buffer and it should work but it doesn't. 在编译器堆栈跟踪中,我看到该函数接收下限Buffer ,它应该可以工作,但不能。

How to fix compile time issue passing right lambda to map function ? 如何解决将正确的lambda传递给map函数的编译时问题?

Try to specify type parameter: 尝试指定类型参数:

websocket
  .flatMap(socket => socket.toObservable)
  .map[String]((msg: Buffer) => {
    msg.toString
  })

or 要么

websocket
  .flatMap(socket => socket.toObservable)
  .map[String](new Function[Buffer, String] {
    override def apply(msg: Buffer): String = {
      msg.toString
    }
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数表达式的类型与形参类型不兼容,你可能希望研究一个通配符类型,例如`_ &lt;: Any`。 (SLS 3.2.10) - argument expression's type is not compatible with formal parameter type,You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10) 在Scala中将类型参数的类型参数用作字段类型 - Use type parameter's type argument as field type in Scala 方法参数类型忽略Scala不变泛型类型参数,具体取决于参数是文字表达式还是变量 - Scala invariant generic type parameter ignored by method parameter type depending whether argument is literal expression versus variable 使用与参数类型相同的类型参数和带有匹配表达式的参数类型 - Using same type parameter as argument type and parameter type with match expression 如何在scala中通过类型参数的类型成员约束参数? - How to constrain argument by type member of type parameter in scala? Scala隐式参数的类型查找失败 - Scala implicit argument's type finding fail Scala类型系统,通过自有类型的参数约束成员的类型 - Scala type system, constrain member's type by parameter of own type 如何在Scala中访问类的类型参数 - How to access a class's type parameter in Scala Scala:不是合法的形式参数 - Scala: not a legal formal parameter 将Scala中的类型作为参数传递 - Passing type in Scala as an argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM