can a scala self type enforce a case class type

痴心易碎 提交于 2019-11-28 12:39:17

This would only work using (as you suggest) structural types, and also only for a fixed known sequence of case class argument types (you need the exact signature of copy). For example:

trait InCase[Repr] {
  self: { def copy(foo: Int): Repr } =>

  def test(foo: Int): Repr = copy(foo)
}

case class Fail(foo: Int, bar: String) extends InCase[Fail] //illegal inheritance

case class Succeed(foo: Int) extends InCase[Succeed]

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