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

Jermaine Oppong on December 27, 2018

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 d... [Read Full]
markdown guide
 

Nice practical intro.

Just to add some terms, in case people wish to look further. The overall concept here are "parametric types" and "parametric functions", where "parameter" is referring to a type, as opposed to an "argument" which refers to a value to a function.

The term "generics" generally refers to parametric container types, and some limited functions. It's a useful, but perhaps less-than-complete form of parametric types, as the implementations usually have many limitations. Full parametric allow a kind of meta-programming, C++ style. Whether this is better or not depends greatly on the experience of the programmer.

Dynamically typed languages don't need parametric types as any container or function, can take any type at all. Though there's a related concept known as duck-typing that applies.

 

+1 for helping me make the connection that K,V is key,value

 
- This is common across object-oriented languages
+ This is common across STATICALLY TYPED object-oriented languages
 
 

Thanks for the article. This reminds me of a code review I was part of.

It was C++ code, involving some templates. Something like this:

template <
    class C, // Something meaningful
    class R, // Something else meaningful
    class D, // Something explaning D well
    class H  // And again
>
class MyClass {
// ... quite some quantity of code...
};

I asked the question if they thought about removing the comments and use meaningful names instead. The answer was: "yes we thought about it, but decided not to".

Well...

Should code read like well-written prose?

 

Great article, never really had the courage to deep dive into Dart but I'll do one day for sure! One thing that bothers me when writing things like

class Cache<T> {}

and then reading later, or in a comment

T meaning Type

Is that often when you say meaning, it really means that you could have just written it full length and not say that (I'm not talking about you personnally, just this kind of convention in general). Why not just write directly

class Cache<CacheType> {
  public addItem(item: CacheType): void { /* ... */ }
  public getItems(): Array<CacheType> { /* ... */ }
}

So that it gets clearer for newcomers or people getting into this code someday. I like to think that my code is a form of art that another person will have to continue after me.

 

Hey Amin,

You can use multi-letter placeholders if you want, like the Microsoft docs, although they still prefixed the placeholder letters docs.microsoft.com/en-us/dotnet/cs...

Personally I would be careful doing CacheType as that feels like there is a class CacheType somewhere, especially looking at addItem(item: CacheType)

 

I see, hence the prefix like TCache. I never really understood that until now. Thanks Jermaine!

 

Amazing explanation for starters, especially when it comes to Generics! Very easily (and in simple words) explained with various examples.
Is it possible to also add type parameters such as N, S, U and V? As these may be used as well!

 

V is there already. Also, read article again! It clearly says this 😉

Simplest answer is convention. In fact you can use any letters you like, achieving the same effect.

 

Talking about S, U and V as in the context of 2nd, 3rd and 4th type. Should have provided that with my question.

 

Thanks! I've been using <T, U, V, ...> when i wrote generic types. Didn't put a second thought into which letters should be used (T for Type, U because T was taken, etc).

Any reason why we don't use a multi-letter word, example: Record<KEY,VALUE> or List<ELEMENT>?

 

I don't have a solid reason as to why multi-letter words are not used, apart from the fact that its more characters and therefore not conventional. Consequently, this could be mistaken for actual rather than placeholder types.

 

In C# you see multi element words, like TElement or TEntity for example.

Yeah you're right. I had seen that in the docs. It's still got the placeholder letter as prefix:

public interface ISessionChannel<TSession>
{
    TSession Session { get; }
}

docs.microsoft.com/en-us/dotnet/cs...

 

Remember Dart 2.1 has int-to-double conversion? I tried this with the Tween where T is dynamic, so its either 1.0 or 0.0 or a widget, and gave me an error to the flutter engine.

For some cases, I still have doubts to use Generics.

 
 
 

Good article for beginners and answering the question in the title, I don't remember any occasion I was stumped by that, I think it just clicked to me.

 

Happy to see it just clicked. Took me a couple of attempts to grasp.

 

Thanks for the article :D.

I have a question if you don't mind, What should I do when I have two types? Should I use T1 and T2?

 

You would commonly do <T, U>, but really any letters can be used. I like the .NET constraints detailed here.

code of conduct - report abuse