enrich-my-library

How do I apply the enrich-my-library pattern to Scala collections?

≯℡__Kan透↙ 提交于 2019-11-26 01:55:54
问题 One of the most powerful patterns available in Scala is the enrich-my-library* pattern, which uses implicit conversions to appear to add methods to existing classes without requiring dynamic method resolution. For example, if we wished that all strings had the method spaces that counted how many whitespace characters they had, we could: class SpaceCounter(s: String) { def spaces = s.count(_.isWhitespace) } implicit def string_counts_spaces(s: String) = new SpaceCounter(s) scala> \"How many