- What does [T] mean after
func1, and why do we put it there?
The [T] in func[T] defines a type parameter T. Your function can be called like func[String]("Hello"), in which String is replaced with T. You can also call it like func("Hello") because the Scala compiler is smart enough to infer that T must be String.
So why do we have to write func[T] when we define it? We need the distinction between arguments of a type given by a type parameter, and arguments given by an actual type. If you write it this: def func1(a : T) : T = a, then T has to be an actual type. For example:
class T
def func1(a : T) : T = a // <-- This compiles now
- Why do we do the same with classes?
You often want to contain an object of some type inside a class. If you define the type parameter at the class level, the type will remain the same throughout your class. Consider this example:
class Container[T](val t: T) {
def isValueEqual(obj: T): Boolean = t.equals(obj)
}
Here, the T in obj: T is the same type as the T defined in Container[T]. Now consider this example:
class Container[T](val t: T) {
def isValueEqual[T](obj: T): Boolean = t.equals(obj)
}
Notice that I defined a new type parameter at the method level as well (isValueEqual[T]). In this case, the T defined in the method will shadow the T defined on the class level. This means that they might not be the same type! You could call it like this:
val c = new Container("Hello")
println(c.isValueEqual(5)) // 5 is not of type String!
MyClassmight be specialized... you might at times haveMyClass[String]and some otherMyClass[Boolean]