how to extend Stream by implementing tailDefined

旧巷老猫 提交于 2019-12-13 02:04:02

问题


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] {

}

When I try this, it tells me tailDefined is protected:

class S[T](s:Stream[T]) extends Stream[T] {
  def tailDefined = s.tailDefined
}

How do I get around this limitation and implement an extension of Stream?


回答1:


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


来源:https://stackoverflow.com/questions/32490665/how-to-extend-stream-by-implementing-taildefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!