I am searching for a way to restrict my polymorphic class to types that have a certain member function.
class Table[T](bla: Array[T]) {
val symbols = bla
symbols.foreach( x => x * probability(x))
def probability(t: T) : Double = ...
}
This code does not compile because T doesnt have member *. How can I assure this. I dont want to use inheritance.
Edit: probability is actually implemented. It returns a Double.
Any ideas?
The problem can be solved in different ways. For example, if you just want type T to have some method (and you don't care whether this method defined on the object or there is implicit conversion that coverts object to something that has this method), then you can use view bounds. Here is an example that expects type T to have method def *(times: Int): T:
class Table[T <% {def *(times: Int): T}](bla: Array[T]) {
bla.foreach( x => println(x * 2))
}
new Table(Array("Hello", "World"))
// Prints:
// HelloHello
// WorldWorld
String does not have method *, but there exist an implicit conversion to StringOps that has this method.
Here is another example. In this case I restricting type T with method def size: Int:
class Table[T <% {def size: Int}](bla: Array[T]) {
bla.foreach( x => println(x.size))
}
new Table(Array(List(1, 2, 3), List("World")))
// Prints:
// 3
// 1
List has method size, and it also works as expected.
But this could be more involving if you are working with numeric values like ints, floats, doubles, etc. In this case I can recommend you to use context bound. Scala has Numeric type class. You can use it to work with numbers without knowledge about their type (with Numeric you can actually work with anything that can be represented as number, so your code would be much more general and abstract). Here is an example if it:
import math.Numeric.Implicits._
class Table[T : Numeric](bla: Array[T]) {
bla.foreach( x => println(x * x))
}
new Table(Array(1, 2, 3))
// Prints:
// 1
// 4
// 9
new Table(Array(BigInt("13473264523654723574623"), BigInt("5786785634377457457465784685683746583454545454")))
// Prints:
// 181528856924372945350108280958825119049592129
// 33486887978237312740760811863500355048015109407078304275771413678604907671187978933752066116
As you noted in comments, Numeric still does not solve your problem, because it can only work on the numbers of the same type. You can simply solve this problem by introducing new type class. Here is an example of it:
import math.Numeric.Implicits._
trait Convert[From, To] {
def convert(f: From): To
}
object Convert {
implicit object DoubleToInt extends Convert[Double, Int] {
def convert(d: Double): Int = d.toInt
}
implicit object DoubleToBigInt extends Convert[Double, BigInt] {
def convert(d: Double): BigInt = d.toLong
}
}
type DoubleConvert[To] = Convert[Double, To]
class Table[T : Numeric : DoubleConvert](bla: Array[T]) {
bla.foreach( x => println(x * implicitly[DoubleConvert[T]].convert(probability(x))))
def probability(t: T) : Double = t.toDouble + 2.5
}
new Table(Array(1, 2, 3))
new Table(Array(BigInt("13473264523654723574623"), BigInt("5786785634377453434")))
With DoubleConvert type class and T : Numeric : DoubleConvert context bound you are not only saying, that T should be some kind of number, but also that there should exist some evidence, that it can be converted from Double. You are receiving such evidence with implicitly[DoubleConvert[T]] and then you are using it to convert Double to T. I defined Convert for Double -> Int and Double -> BigInt, but you can also define you own for the types you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With