構文を使用するために必要なimplicitsと型の制約を整理しようとするのではなく、NumericRange.inclusive
を呼び出すほうが簡単です。暗黙的にIntegral
が必要な場合は、範囲を1ステップで構成する方法を知ります(Integral
インスタンスによって提供されます)。 (あなたは型制約を持つにそれらをしたい場合)
import scala.reflect.ClassTag
import scala.collection.immutable.NumericRange
def foo2[A >: Int with Char : ClassTag](tuple: (A, A))(implicit int: Integral[A]) =
NumericRange.inclusive(tuple._1, tuple._2, int.one).toArray
scala> foo2(('a', 'z'))
res13: Array[Char] = Array(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
scala> foo2((1, 10))
res14: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
その他の失敗:我々はまた、一般的にArray
を作成するClassTag[A]
が必要になります
scala> foo2((1L, 10L))
<console>:19: error: could not find implicit value for parameter int: Integral[AnyVal]
foo2((1L, 10L))
^
'T字型'の下界を「Char with Int」と呼んでいないのですか?それは何をしようとしていますか? –
申し訳ありません、編集済みです。 – jalv1039
あなたは私が思う組合タイプが欲しいです。 http://stackoverflow.com/questions/3508077/how-to-define-type-disjunction-union-types –