如何通过实现tailDefined来扩展Stream [英] how to extend Stream by implementing tailDefined
本文介绍了如何通过实现tailDefined来扩展Stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想扩展 scala.Stream
.当我尝试时,它告诉我我不能,因为我没有所需的方法 tailDefined
.
I'd like to extend scala.Stream
. When I try, it tells me I can't, because I don't have the required method tailDefined
.
class S[T](s:Stream[T]) extends Stream[T] {
}
当我尝试这个时,它告诉我 tailDefined
是受保护的:
When I try this, it tells me tailDefined
is protected:
class S[T](s:Stream[T]) extends Stream[T] {
def tailDefined = s.tailDefined
}
如何绕过这个限制并实现 Stream
的扩展?
How do I get around this limitation and implement an extension of Stream
?
推荐答案
如果你想添加新方法"到 Stream
,使用隐式类:
If you want to "add new methods" to Stream
, use implicit classes:
implicit class S[T](s:Stream[T]) {
def method1 = ...
}
val s: Stream[Int] = ...
s.method1
这篇关于如何通过实现tailDefined来扩展Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文