B) ...">

What does the "It" symbol mean in Scala

I saw some code examples using the “That” character with Generics. those.

def map[B, That](f : (A) => B) : That 

But due to Google’s lack of ability to use this word, I can’t find any documentation about what it does or how I use it.

Is it just a regular type placeholder or is it doing something special?

+6
source share
3 answers

Any identifiers inside [...] considered as type parameters.

Therefore, in the case of def map[B, That](f : (A) => B) : That That , only the general return type is used. Replace it with Z , for example: def map[B, Z](f : (A) => B) : Z will have the same effect.

+13
source

Translation in Java, which will look like this:

 public <B, That> That map(f: Function1<A, B>) 

Or, in other words, That is a generic (or in Scala lingo, type parameter).

+3
source

This is the "placeholder" as you call it (i.e. type parameter); the convention in the collection library should use That to represent the type of collection being created. (This way you convert from this to That .)

+3
source

All Articles