1

我在从我的 Scala 代码(scala 2.10.4)调用 Jedis(Java)时遇到了一些互操作问题。

我有一个在隐式类中实现的特征

在基础类上调用方法时出现以下错误

implicit class Redis(private val underlying: Jedis) extends com.dy.storage.Redis {
    override def del(keys: String*): Long = {
      underlying.del(keys: _*)
    }

    override def del2(keys: Seq[Array[Byte]]): Long = {
      underlying.del(keys: _*)
    }
}

收到以下错误(scala 2.10.4):

no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error]       underlying.del(keys: _*)
[error]                          ^
[error] /Users/avnerbarr/workspace/redis-hashed-no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error]       underlying.del(keys: _*)
[error]                          ^

我尝试添加一个包装器并将有问题的调用移到隐式类之外:

    override def del(keys: String*): Long = {
      Wrapper.delete(underlying, keys)
    }


object Wrapper {
  def delete(u : Jedis, str: Seq[String]): Long = {
    u.del(str:_*)
  }
}

然后出现此错误:

overloaded method value del with alternatives:
[error]   (x$1: String)Long <and>
[error]   (x$1: <repeated...>[String])Long <and>
[error]   (x$1: Array[Byte])Long <and>
[error]   (x$1: <repeated...>[Array[Byte]])Long
[error]  cannot be applied to (Char)
[error]     u.del(a:_*)
[error]       ^
[info] Checking every *.class/*.jar file's SHA-1.
[error] one error found

此代码在使用 scala 2.11 时完全没有问题,并且仅在 scala 2.10 中出现问题

是否有可能“选择”而不是推断应该调用的正确方法?

4

1 回答 1

0

您应该提供Jedis和的代码com.dy.storage.Redis

以下代码编译:

src/main/java/Jedis.java

interface Jedis {
  long del(String... keys);
  long del(byte[]... keys);
}

src/main/scala/com/dy/storage/Redis.scala

package com.dy.storage

trait Redis {
  def del(keys: String*): Long
  def del2(keys: Seq[Array[Byte]]): Long
}

src/main/scala/App.scala

object App {
  implicit class Redis(private val underlying: Jedis) extends com.dy.storage.Redis {
    override def del(keys: String*): Long = {
      underlying.del(keys: _*)
    }

    override def del2(keys: Seq[Array[Byte]]): Long = {
      underlying.del(keys: _*)
    }
  }
}

构建.sbt

name := "scala2104"
version := "0.1"
scalaVersion := "2.10.4"
于 2018-02-15T16:17:01.317 回答