I am learning Context bound in Scala.
In the below code, I am invoking multiplication operator on the integer parameter. But it errors out. 'a' is considered as type parameter; but it is actually not as per my understanding. Can someone please help.
scala> class Sample[T]
defined class Sample
scala> def method[Int:Sample](a:Int) = a * a
<console>:12: error: value * is not a member of type parameter Int
def method[Int:Sample](a:Int) = a * a
Thanks!
The type parameter named
Intdoes not represent concrete integer typescala.Int. Instead it is just a confusing coincidence that the type parameter was given the same nameIntas the concrete type. If you give it some other name such asTthe error message should make more sense. Now we see
*is not defined forTsinceSampletype class does not yet provide such capability. Here is an example of how correct syntactic usage might look usageYou could also have a look at
Numerictype class which provides such functionality out of the box