Getting to grips with Generics.

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Getting to grips with Generics.

Post by Phil Hepburn »

Hi guys,

Well, its been a long time coming (years and years) but I am now in a position to start researching Generics as it is used in the .NET world.

Yes, some of us have used it with collections like 'List<int>' but for technical reasons could not make definitions ourselves, using the full Generics capabilities.

Now we have Roslyn then xBase guys can start from scratch and do Generics properly.

I have made a start and feel as if I am doing well, and I will document my coding successes in part of my "Appendix 'X' " which I am currently compiling.

I am trying to make Generics simple and straight forward, which it is, by finding the right documentation and suitable examples. I am winning I feel ;-0)

If you can manage parameters in Classes and Methods (which we do all the time) then we can also manage 'Types' in them as well. 'Types' is a word used when some of us may prefer to use 'Classes'. So when in documentation we read "Types and Methods" we can think of it as "Classes and Methods".

Okay then, lets make a start, parameters are provided in round brackets ' ( ) ' and now with Generics the Types are provided in chevrons, or pointed brackets, ' < > '.

We can have one or more Type provided, and these can be named other than 'T', or 'T1' or 'TVal' etc. Its just convention. Any text item in a comma separated list in the '< >' is taken as a Type.

Lets see an example I have had working earlier this afternoon :-
GenericsExplained_01.jpg
GenericsExplained_01.jpg (78.16 KiB) Viewed 311 times
Notice that in the three lines to define the 'class' or 'Type' we have different signatures. Like our regular 'stuff' we can't have the same signature twice over. And, the names are convention, possibly quite arbitrary, and our own choice. I would stick to 'T' in many cases, as this is what we read in Wen samples.

Lets now see a Method, both in use and its definition :-
GenericsExplained_02.jpg
GenericsExplained_02.jpg (24.08 KiB) Viewed 311 times
GenericsExplained_03.jpg
GenericsExplained_03.jpg (55.4 KiB) Viewed 311 times
So for Generic classes (Types) we usually have both lists of Types in ' < > ' and also lists of parameters in ' ( ) '.

And yes we can have overloads, as with Class constructors and Method definitions.

Finally for the moment, I include an image of Robert's sample from the X# CHM file, with some modifications by myself, to suit what I am trying to do.

Notice on line 186 I have followed Wolfgang's advice on property definition syntax. Thanks 'WR'.
GenericsExplained_04.jpg
GenericsExplained_04.jpg (116.36 KiB) Viewed 311 times
Before I go, it would seem that the point of Generics, is the same as Procedures, to reuse code and stop code 'bloat' by repeating similar code. As yet I have not a great or good example to demonstrate this, but you may get my point if you compare Generics with Type provision, to Methods and Procedures using data parameters.

Hope this may start an interesting thread, I do have more nice samples, but enough for now.

Cheers, and Best regards,
Phil.
Wales, UK.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Getting to grips with Generics.

Post by Phil Hepburn »

Hi guys,

Just to keep you informed, the research work I am conducting on Generics in .NET and finding suitable X# syntax, is going well. The topic seems a lot clearer and simpler to me now I have put in the work and experimentation / testing.

I have added some sections to the Cologne 2017 eNotes - see image below :-
GenericsExplained_11.jpg
GenericsExplained_11.jpg (45.02 KiB) Viewed 311 times
It would seem to me that many of us coming new to Generics probably are best advised to follow up and use the Types already included in the current recent version of .NET - I mean those in System.Collections. Generic and System.Collections.ObjectModel.

If we don't go and see what these Namespaces offer us then we may all be reinventing the wheel, badly ;-0)

Has any of you used 'Laz<T>', and is so have you a clear and straight forward example to share with us ?

Finally, has any of you got a good / reasonable example of your own Generics Class, Method or other member ? What I find in eBooks and web articles all seem to simple, trivial, and no real hep or use at all. yes, they work, but that is a very different matter.

HELP !!!

Cheers,
Phil.
Wales, UK.
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Getting to grips with Generics.

Post by robert »

Phil,

Roslyn uses Lazy<> all over the place. For example when it loads the types from an external assembly it does not get all the info about its methods, fields and properties. If the type is never used, why bother reading all of that info. So this info is exposed with Lazy properties. Only when you are requesting the info then it will create the info for you and return it.

That is probably one (of the many) tricks that makes Roslyn, and therefore also the X# compiler, so fast.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Getting to grips with Generics.

Post by Frank Maraite »

Hi Phil,

I tried generics recently and finally skipped/delete all I've done so far.

Using generics has two requirements
- totally understanding of interfaces
- similar actions on different types

That's why generics on lists are relativly easy to implement.

I think we can use generics after implementing different classes with not only the same interface but also the same code. The usual copy and paste situation. Then we can replace the always same code of the different classes with a generic one.

Just short thoughts about it. Hope I'm right in some way.

Cheers
Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Getting to grips with Generics.

Post by Phil Hepburn »

Hi Frank and all,

Remember :- Classes and Types are the same thing ...

At its basic (fundamental) level, Generics is simply "Type passing", in the same way that use of input parameters in Classes, Methods, and other members, is "data item passing".

In the ' < > ' we pass in Types, and in the ' ( ) ' we pass in data items.

So we can overload a Type signature with Generics as we do a parameter signature with data items.

Now then, what we then do with the Types we pass in is another matter.

Too often in the web notes and eBook the details of what works in Generics is put in front (before) what Generics actually is, or even what it is best used for.

The stuff I have so far added to my Cologne eNotes is VERY surprisingly simple or straight forward - I had expected something difficult.

I suspect the difficult part is going to be finding out how we as coders should use it.

Meanwhile I will use the .NET framework Generic types as much as possible.

Oh! the performance of strongly typed Collections is far better than that when they are non-Generic. All to do with boxing of collection items and the likes.

Best regards,
Phil.
Wales, UK.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Getting to grips with Generics.

Post by NickFriend »

Hi Frank,

True. As a rule of thumb, if I find I have a fairly compact specific class, and I'm copying it more than a couple of times and just changing the classes that I'm using in it, I start to look for a generic reworking of it. This has saved me literally thousands of lines of coding.

If the only thing you're doing is say storing an object in a property of the class, then not much is involved. But if you need to start actually carrying out operations, then the problem is how to guarantee that the classes you're passing in as type parameters support the required operations - and this of course is where interfaces come in. As you know, if a class implements an interface it's guaranteed to implement the methods and properties specified in that interface.

So in your generic class or method definition, the where clauses can be used to specify which interfaces must be implemented, and thus you can start to put specific method calls into your generic class. And this is when generics really start to become useful.

Nick
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Getting to grips with Generics.

Post by wriedmann »

Hi Phil,

I for myself like generics, as they make code stabler and permit the reuse of methods and classes. At least in the collection classes they are used over and over.

I have to admit that even if I'm slowly feeling "at home" in the .NET framework with all its possibilities, I'm an absolute beginner..... Remembering the famous Socrates word "I know that I know nothing" all the time.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Frank Maraite
Posts: 176
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Getting to grips with Generics.

Post by Frank Maraite »

Hi Phil,

you're right, I use strongly typed collections very often. I do (mostly, whenever I remember I should do so)

sealed class MyCollection inherit ...Generic.Collection<myItemClass>
end class

local MyCollection as MyCollection

This way I have only one place to change the collection type if needed. In fact, at the place of usuage it's not clear that this is a generic.

I another post you wrote about interface over doing. I think you can hardly overdo the use of interfaces. They are so essential.

Nick in the meantime wrote about it (I saw in my E-Mail).

Frank
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Getting to grips with Generics.

Post by Phil Hepburn »

Arrrh ! - so you recognised your own profile ;-0)

This makes you half cured to over-using Interfaces then.

My simple point was - just because something is good, and useful, does not mean it has to be used all of the time, anywhere.

I distinctly remember you eulogising about Interfaces at DevShare (maybe Shrewton) when you said everything should be done by Interfaces. You had just discovered them.

See, you need to be careful what you say, when the elephants have their ears outstretched ;-0)

I will get into Interfaces with Generics sometime soon. I promise you. However, remember that passing Types into Interfaces is Generics, its not the function of the Interface which has anything to do with the Generic side of it. The same ' < > ' technology applies to Events and Delegates too.

Hope this makes some sense,
Cheers,
Phil.
P.S. Clocks go forward tomorrow ..... Cologne gets closer.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Getting to grips with Generics.

Post by Phil Hepburn »

Hi Robert,

Yes, LAZY<T> sounds very interesting and useful - BUT - it would seem that for standard guys like myself, we need to go easy before 'WE' start using it all over the place.

If we are to use Entity Framework 6, or later, then the Microsoft guys have already built-in a sensible amount of LAZY<> access for us.

In my recent 'Stock' sample for Cologne 2017, when I get a list of Customers back, the Orders list for each customer is not retrieved until required/asked for. Now I understand why there is a short time lapse when I click the combo / drop-down box for an individual Customer's orders.

It looks as it for many of us just using the latest version of all of the .NET frameworks, that much useful Generics technology is built-in for us.

My feeling is that we should use Generic types, and the latest EF stuff, then see what is lacking, before we rush off and re-invent the wheel etc., etc..

I will attempt to attach and insert a short video which clearly shows this happening at runtime. Fingers crossed :-

[The extension mp4 has been deactivated and can no longer be displayed.]

The only issue / problem with watching the video is that the delay happens after a mouse click BUT the mouse click is not visually obvious ;-0( I think you will get the idea by watching the quick opening of re-visited drop boxes.

Remember that this is all available to me without me having to do anything. EF6 does it all for me.

Hope that helps,
Phil.
Post Reply