Scala - implicit conversion with unapply

眉间皱痕 提交于 2019-12-05 04:13:28

Implicit conversions are not applied when pattern matching. That's not a bug or a problem with your code, it's simply a design decision of the creators of Scala.

To fix it, you should write another extractor that accepts a String — which in turn can call your implicit conversion.

Alternatively, you can try with a view bound, which seems to work as well, and will also work if you later define other implicit conversions to MyString:

object Apply {
  def unapply[S <% MyString](s: S): Option[String] = Some(s.s)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!