0

我有一个重载的 java 方法调用,它可以采用 String 或 InetAddress 可变参数。为了降低复杂性,我想在调用 java 方法之前将字符串序列转换为 InetAddresses 序列。在将字符串转换为 InetAddress 时,我只想允许有效值

在typetagsTry上看到这个后,我想出了:

import scala.reflect.runtime.universe._
import java.net.InetAddress
import scala.util.Try

type InetAddr = java.net.InetAddress

def tryMap[A](xs: Seq[Try[A]]) =
  Try(Some(xs.map(_.get))).
     getOrElse(None)

def toInet(address: String): Try[InetAddr] = {
  Try(InetAddress.getByName(address))
}


def stringToInet[A: TypeTag](xs: Seq[A]): Option[Seq[InetAddr]] = typeOf[A] match {
  case t if t =:= typeOf[String] => tryMap(xs.map(toInet)).getOrElse(None)
  case t if t =:= typeOf[InetAddr] => Some(xs)
  case _ => None
}

但我得到了错误

<console>:18: error: type mismatch;
 found   : String => scala.util.Try[InetAddr]
 required: A => ?
         case t if t =:= typeOf[String] => tryMap(xs.map(toInet)).getOrElse(None)

我不确定我是否接近获得可行的东西或严重偏离轨道,并希望在寻找解决方案方面获得一些指导。

4

1 回答 1

0

问题是,即使您知道 type 标记等于您想要的类型,也不会更改 的类型xs,因此您需要执行以下操作:

  import scala.reflect.runtime.universe._
  import java.net.InetAddress
  import scala.util.Try

  def tryMap[A](xs: Seq[Try[A]]) =
    Try(Some(xs.map(_.get))).
      getOrElse(None)

  def toInet(address: String): Try[InetAddress] = {
    Try(InetAddress.getByName(address))
  }

  def stringToInet[A: TypeTag](xs: Seq[A]): Option[Seq[InetAddress]] = typeOf[A] match {
    case t if t =:= typeOf[String] => 
        tryMap(xs.asInstanceOf[Seq[String]].map(toInet)) // don't need the getOrElse, it's already an option
    case t if t =:= typeOf[InetAddress] => 
        Some(xs.asInstanceOf[Seq[InetAddress]])
    case _ => None
  }

asInstanceOf有点难看,但有必要做你想做的事。

于 2013-07-04T01:47:15.610 回答