6

I am trying to understand the type parameters in Scala. Let's look the following general example:

def func1[T](a : T) : T = a

I understand that func1 takes 1 parameter of any type, and returns that parameter of the very same type. What i don't understand is why:

def func1[T]

Why [T] right after function1?? We could simply write it without [T] after func1, like:

def func1(a : T) : T = a

1) What does that [T] means after func1 and why we put it there?

2) Why we do the same with classes?

class MyClass[T]{...}

I mean MyClass instantiations are of type MyClass. What does [T] means there? You don't say i have a boolean Class of type MyClass, you say i have an object of type MyClass right?

Thanks in advance.

1
  • 1
    On your second point: you could be working with your class that's a container of other types of objects. Easy examples is List[String]. Seemingly your MyClass might be specialized... you might at times have MyClass[String] and some other MyClass[Boolean] Commented Jan 27, 2017 at 17:17

1 Answer 1

9
  1. 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
  1. 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!
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your analysis. 1) Yes i did a mistake. It was T instead of A. I corrected it now. 2) Although i am not sure i totaly understand the concept, i think i just got closer in understanding type parameters in Scala. 3) Because my reputation is less than 15, my vote feedback isn't recorded.
@Spartan It's over 15 now ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.