Ever been stumped by <E>, <T>, <K, V> in OO language documentation?

Ever been stumped by <E>, <T>, <K, V> in OO language documentation?

Over on the Reddit /r/dartlang group, an individual by the name of NFC_TagsForDroid reached out to me in regards to confusion navigating the Dart documentation. This was specifically when understanding the meaning behind some of the “tokens” used when demonstrating code samples.

Here’s an extract from the user’s comment:

Can you please write a explainer on how to read dartlang documentation? Most of it is meaningless to a beginner. For example: api.dartlang.org/stable/2.1.0/dart-core/Lis.. (Dart dart:core List<E> add abstract method)
the heading Lis
t<E> add abstract method what is the <E>? how does it being an abstract method affect anything?

The user is referring to the signature for the List type’s add() method:

void add(
  E value
);

The source of confusion is the E. Others used in various documentation code samples are T, K and V.

So what do these mean?

These seemingly magical “tagged letters” are placeholders that are used to represent what is known as a type parameter. This is common across statically-typed object-oriented languages and will be apparent when it comes to the topic of Generics.

Generics provide a way of telling the compiler what type is being used, so that it knows to check for that.

In other words if you see <E> read it as “of Type“, so List<String> will be read as “List of String“.

Now looking at that let’s say we define a List<E>:

List<String> fruits = ['apple', 'orange', 'pineapple'];

Looking at the add method again:

void add(
  E value
);

The way to read this is that the E represents an element in the collection, whatever type we initially specified when we created that list! In the case of fruits its String.

And this is how we would use it:

fruits.add('mango');

fruits.add(1); // This will throw an error as it's the wrong type

So why are particular letters used?

Simplest answer is convention. In fact you can use any letters you like, achieving the same effect. However the common ones carry semantic meaning:

  • T is meant to be a Type

  • E is meant to be an Element (List<E>: a list of Elements)

  • K is Key (in a Map<K, V>)

  • V is Value (as a return value or mapped value)

This code below will work even when I don’t use any of the placeholder letters above. For example, see this snippet below:

class CacheItem<A> { // Using the letter A
  CacheItem(A this.itemToCache);

  final itemToCache;

  String get detail => '''
    Cached item: $itemToCache
    The item type is $A
  ''';
}

void main() {
  var cached = CacheItem<int>(1);
  print(cached.detail);
  // Output:
  // Cached item: 1
  // The item's type is int
}

This works although the placeholder used is A. By convention T would be used:

class CacheItem<T> {
  CacheItem(T this.itemToCache);

  final itemToCache;

  String get detail => '''
    Cached item: $itemToCache
    The item type is $T
  ''';
}

Generics are powerful in that they allow us to reuse the same class with various types:

CacheItem<String> cachedString = CacheItem<String>('Hello, World!');
print(cachedString.detail);
// Output:
// Cached item: Hello, World!
// The item's type is String

var cachedInt = CacheItem<int>(30);
print(cachedInt.detail);
// Output:
// Cached item: 30
// The item's type is int

var cachedBool = CacheItem<bool>(true);
print(cachedBool.detail);
// Output:
// Cached item: true
// The item's type is bool

Hope this was insightful and you learn something new today in your software engineering journey.

Further reading