Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何按子类型B<;筛选类似于流[Future[a]]的类<;:A._Scala_Type Erasure_Type Parameter - Fatal编程技术网

Scala 如何按子类型B<;筛选类似于流[Future[a]]的类<;:A.

Scala 如何按子类型B<;筛选类似于流[Future[a]]的类<;:A.,scala,type-erasure,type-parameter,Scala,Type Erasure,Type Parameter,我有一个类来管理异步消息流的接收。它有一个类型参数,比如说A,我喜欢实现一个按类型过滤这些消息的函数,比如说B def next():未来[A] def过滤器[B未来。成功(B) 案例=>next() } } } 案例类MessageStreamImpl[A](msg:IndexedSeq[A])扩展了MessageStream[A]{ 私人var指数=0 def next()={ 指数+=1 Future.successful(msg(索引-1)) } } 对象主体{ 特征A 案例类别B(i:

我有一个类来管理异步消息流的接收。它有一个类型参数,比如说
A
,我喜欢实现一个按类型过滤这些消息的函数,比如说
B
def next():未来[A]
def过滤器[B未来。成功(B)
案例=>next()
}
}
}
案例类MessageStreamImpl[A](msg:IndexedSeq[A])扩展了MessageStream[A]{
私人var指数=0
def next()={
指数+=1
Future.successful(msg(索引-1))
}
}
对象主体{
特征A
案例类别B(i:Int)扩展了A
案例类别C(i:Int)扩展了
def main(参数:数组[字符串]){
val msg:IndexedSeq[A]=(1到10).map{i=>if(i%2==0)B(i)else C(i)}
val streamOfA=MessageStreamImpl(msg)
val streamOfB=streamOfA.filter[B]
val b:b=Await.result(streamOfB.next(),1秒)
}
}
编译时,我得到
警告:抽象类型模式B被取消选中,因为它被擦除消除了
,而且代码确实不工作。如果我执行Main,我会得到错误:
lang.ClassCastException:test.filterByType.Main$C不能强制转换为test.filterByType.Main$B


发生这种情况是因为它没有过滤掉第一个列表项
C(1)

这个小的调整起到了作用

import scala.reflect.ClassTag
def filter[B<:A](implicit C: ClassTag[B]): MessageStream[B] = new MessageStream[B]{
    def next(): Future[B] = self.next.flatMap{
        case b: B => Future.successful(b)
        case _    => next()
    }
}
导入scala.reflect.ClassTag
def过滤器[B未来。成功(B)
案例=>next()
}
}

这个小小的调整就达到了目的

import scala.reflect.ClassTag
def filter[B<:A](implicit C: ClassTag[B]): MessageStream[B] = new MessageStream[B]{
    def next(): Future[B] = self.next.flatMap{
        case b: B => Future.successful(b)
        case _    => next()
    }
}
导入scala.reflect.ClassTag
def过滤器[B未来。成功(B)
案例=>next()
}
}

我找到了一个可行的替代方案:

def collect[B<:A](f: PartialFunction[A,B]) = new MessageStream[B] {
  override def next(): Future[B] = self.next().flatMap { m =>
    if (f.isDefinedAt(m)) Future.successful(f(m))
    else next()
  }
}
def收集[B]
如果(f.isDefinedAt(m))未来成功(f(m))
else next()
}
}

我找到了一个可行的替代方案:

def collect[B<:A](f: PartialFunction[A,B]) = new MessageStream[B] {
  override def next(): Future[B] = self.next().flatMap { m =>
    if (f.isDefinedAt(m)) Future.successful(f(m))
    else next()
  }
}
def收集[B]
如果(f.isDefinedAt(m))未来成功(f(m))
else next()
}
}

我以前见过这个
ClassTag
的东西。你能解释一下为什么它能工作吗?a
ClassTag[T]
存储给定类型的已擦除类
T
。因此本质上它是为了解决您所面临的问题。我以前见过
ClassTag
这件事。您能解释一下为什么它会起作用吗?a
ClassTag[T]
存储给定类型的已擦除类
T
。因此,本质上它是为了解决您所面临的问题。