Q
如何在斯卡拉
8
A
回答
9
不是因爲你把它真的有可能,但你可以使用類型類模式去做。例如,從here:
sealed abstract class Acceptable[T]
object Acceptable {
implicit object IntOk extends Acceptable[Int]
implicit object LongOk extends Acceptable[Long]
}
def f[T: Acceptable](t: T) = t
scala> f(1)
res0: Int = 1
scala> f(1L)
res1: Long = 1
scala> f(1.0)
<console>:8: error: could not find implicit value for parameter ev: Acceptable[Double]
f(1.0)
^
編輯
這個工作,如果類和對象的同伴。在REPL上,如果您在不同的行上鍵入每一行(即在它們之間出現「結果」),則它們不是同伴。你可以像下面鍵入,雖然:
scala> sealed abstract class Acceptable[T]; object Acceptable {
| implicit object IntOk extends Acceptable[Int]
| implicit object LongOk extends Acceptable[Long]
| }
defined class Acceptable
defined module Acceptable
1
有了這個黑客:
implicit val x: Int = 0
def foo(a: List[Int])(implicit ignore: Int) { }
implicit val y = ""
def foo(a: List[String])(implicit ignore: String) { }
foo(1::2::Nil)
foo("a"::"b"::Nil)
見http://michid.wordpress.com/2010/06/14/working-around-type-erasure-ambiguities-scala/
而且也是這個question。
5
您可以從任一種類型中獲得一小部分里程。然而,Either層次結構是密封的,處理兩種以上類型變得麻煩。
scala> implicit def string2either(s: String) = Left(s)
string2either: (s: String)Left[String,Nothing]
scala> implicit def int2either(i: Int) = Right(i)
int2either: (i: Int)Right[Nothing,Int]
scala> type SorI = Either[String, Int]
defined type alias SorI
scala> def foo(a: SorI) {a match {
| case Left(v) => println("Got a "+v)
| case Right(v) => println("Got a "+v)
| }
| }
foo: (a: SorI)Unit
scala> def bar(a: List[SorI]) {
| a foreach foo
| }
bar: (a: List[SorI])Unit
scala>
scala> foo("Hello")
Got a Hello
scala> foo(10)
Got a 10
scala> bar(List(99, "beer"))
Got a 99
Got a beer
2
另一種解決方案是包裝類:
case class IntList(l:List[Int])
case class StringList(l:List[String])
implicit def li2il(l:List[Int]) = IntList(l)
implicit def ls2sl(l:List[String]) = StringList(l)
def foo(list:IntList) = { println("Int-List " + list.l)}
def foo(list:StringList) = { println("String-List " + list.l)}
3
相關問題
- 1. 如何在斯卡拉
- 2. 如何在斯卡拉
- 3. 如何在斯卡拉
- 4. 如何在斯卡拉
- 5. 如何在斯卡拉
- 6. 如何在斯卡拉
- 7. 如何在斯卡拉
- 8. 如何在斯卡拉
- 9. 如何在斯卡拉
- 10. 如何寫在斯卡拉
- 11. 如何在斯卡拉
- 12. 如何在斯卡拉
- 13. 如何在斯卡拉
- 14. 如何在斯卡拉
- 15. 如何在斯卡拉
- 16. 如何在斯卡拉
- 17. 如何在斯卡拉
- 18. 如何在斯卡拉
- 19. 如何在斯卡拉
- 20. 如何在斯卡拉
- 21. 如何在斯卡拉
- 22. 如何在斯卡拉
- 23. 如何在斯卡拉
- 24. 如何在斯卡拉
- 25. 如何在斯卡拉
- 26. 如何在斯卡拉
- 27. 如何從斯卡拉(卡斯巴)
- 28. 斯卡拉在斯卡拉類
- 29. 在斯卡拉
- 30. 在斯卡拉
謝謝,也引用http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf – oluies 2010-09-24 21:58:37
@Brent正如我通過電子郵件所說的,這可能是因爲在不同的行上輸入對象。看我的編輯。 – 2010-09-25 20:44:34