Context bounds with two generic parameters

自古美人都是妖i 提交于 2019-12-14 00:48:45

问题


In Scala, I can use context bounds:

def sort[T : Ordered](t: Seq[T])

To mean the same thing as:

def sort[T](t: Seq[T])(implicit def Ordered[T])

What if I have a class with two generic parameters. I.e. I want to be able to ensure that I have a Writer[T, String]. Is there a syntax where I can use context bounds (T : ...) or do I need to have the implicit explicitly (that was fun to write).


回答1:


Yes, it's possible! But not really very pretty:

trait Writer[T, O] {
  def write(t: T): O
}

def writeToString[T: ({ type L[x] = Writer[x, String] })#L](t: T) =
  implicitly[Writer[T, String]].write(t)

implicit object intToStringWriter extends Writer[Int, String] {
  def write(t: Int) = t.toString
}

And then:

scala> writeToString(1)
res0: String = 1

The ({ type L[x] = Writer[x, String] })#L thing is called a type lambda. Sometimes they're very convenient (if buggy), but this definitely isn't one of those times. You're much better off with the explicit implicit.



来源:https://stackoverflow.com/questions/20131589/context-bounds-with-two-generic-parameters

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