Type parameter in scala -


i trying understand type parameter concepts in scala.

def sum [a] (a:a):a={a} // used single parameter , working fine, able pass data type. 

here:

def sum[a](a:a,b:a):a={a+b} //declare 2 arguments  <console>:7: error: type mismatch;  found   :  required: string  def sum[a](a:a,b:a):a={a+b} 

the above function has type parameter, have not mentioned data type - how come it's treated string?

def sum[a](a:a,b:a):a={a+b} 

you adding t + t, compiler can't infer + t, use default implicit +: any2stringadd, compiler throw required: string error.

see https://issues.scala-lang.org/browse/si-8229

  implicit final class any2stringadd[a](private val self: a) extends anyval {     def +(other: string): string = string.valueof(self) + other   } 

Comments