Jak można wprowadzić podtyp liczby numerycznej [T]? Szukałem w przewodniku na ten temat, ale nie znalazłem żadnych. Przykład podtypów może być Racjonalny lub Złożony?Scala: Wdrażanie podtypu numerycznego [T]
góry dzięki Troels
Jak można wprowadzić podtyp liczby numerycznej [T]? Szukałem w przewodniku na ten temat, ale nie znalazłem żadnych. Przykład podtypów może być Racjonalny lub Złożony?Scala: Wdrażanie podtypu numerycznego [T]
góry dzięki Troels
absolutnie bezużyteczny ciąg liczbowy:
trait StringIsNumeric extends Numeric[String] {
def plus(x: String, y: String): String = "%s+%s" format (x, y)
def minus(x: String, y: String): String = "%s-%s" format (x)
def times(x: String, y: String): String = "%s*%s" format (x, y)
def quot(x: String, y: String): String = "%s/%s" format (x, y)
def rem(x: String, y: String): String = "%s%%s" format (x, y)
def negate(x: String): String = "-%s" format (x)
def fromInt(x: Int): String = x.toString
def toInt(x: String): Int = 0
def toLong(x: String): Long = 0L
def toFloat(x: String): Float = 0.0f
def toDouble(x: String): Double = 0.0
}
implicit object StringIsNumeric extends StringIsNumeric with Ordering.StringOrdering
def x[T: Numeric](t1 : T, t2 : T) = {
val n = implicitly[Numeric[T]]
import n._
t1 * t2
}
scala> x("a","b")
res0: java.lang.String = a*b
dodałem Real do Scalaz z wystąpień Real[Double]
i Real[Dual]
.
znalazłem to wygodne, aby fromDouble
niejawny
Próbujesz utworzyć instancję liczbową konkretnego T, (np ComplexNumeric), albo bardziej ogólnego typu numerycznej (np rzeczywistym [T])? – retronym
Cóż, myślę, że to, czego chcę, to instancja Numeric. Moim dużym problemem jest to, że nie byłem w stanie znaleźć dokumentacji przewodnika na temat korzystania z nowego typu numerycznego [T]. Czy taki przewodnik istnieje? –