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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
I like this fix. Merged.
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>
orList<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
orTEntity
for example.Yeah you're right. I had seen that in the docs. It's still got the placeholder letter as prefix:
docs.microsoft.com/en-us/dotnet/cs...
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
and then reading later, or in a comment
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 directlySo 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 aclass CacheType
somewhere, especially looking ataddItem(item: CacheType)
I see, hence the prefix like
TCache
. I never really understood that until now. Thanks Jermaine!You're welcome Amin.
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:
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?
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.
V
is there already. Also, read article again! It clearly says this 😉Great intro Jermaine!
Thanks David!
Good article.thx
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.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.
Sure, you can do that.