Tuesday, December 10, 2013

TEACHING: Study club of MUGI Jadetabek on Developer Track, WEEK 4

Now, it’s week 4! We could not have the workshop within the next week of week 3 because of my busy schedule, sorry!

Also in the week 4, we decided to meet at QQ Kopitiam, at Pacific Place at 9:30 because I had something to do first in the morning. Not just my schedule, but the study club will need to adjust with the participants. Therefore this week 4 was commenced on 7th December, 2013.

To my surprise, the attendance were few: Wakhid and Aris weren’t present. But Wakhid was able to tell me that he’s still minding his final paper assignment.

Ok, the show must go! Sorry to leave you if you weren’t present, guys!

This week I reviewed the knowledge of basic generic and collection. To my surprise, not all of them understood and grabbed the full concept of last week.

WP_000182_thumb_5D41B33E

The assignment was to create a simple implementation of LINQ’s WHERE and also implement ICollection<T>, so I decided to repeat the explanation on how generic works.

Basically we can consider generic type as parameterized type or type as parameter. So, if we declare ICollection<T>, we could use and implement derived ICollection with String as parameter (or any type).

The type T then can be replaced by String, so the use will be ICollection<String> (read as ICollection of String). In fact, arrays in .NET 2.0 and above are implemented as collections of parameterized type, so when we have array of bytes, it’s actually implemented as collections of strongly typed bytes, not just array of boxed objects.

Sample from MSDN: (from http://msdn.microsoft.com/en-us/library/vstudio/b5bx6xee%28v=vs.110%29.aspx)

// The .NET Framework 1.1 way to create a list:
System.Collections.ArrayList list1 = new System.Collections.ArrayList();
list1.Add(3);
list1.Add(105);

System.Collections.ArrayList list2 = new System.Collections.ArrayList();
list2.Add("It is raining in Redmond.");
list2.Add("It is snowing in the mountains.");

Although it’s very subtle, the ArrayList will cast to objects, and this is called boxing.

As consequences, we also need to understand boxing and unboxing concept in C# and VB. For more info, visit boxing and unboxing in MSDN Library.

Now with .NET 2.0 and above, we can create list with strong typed:

// The .NET Framework 2.0 way to create a list
List<int> list1 = new List<int>();

// No boxing, no casting:
list1.Add(3);

// Compile-time error:  // list1.Add("It is raining in Redmond.");

Now every time we access each or any member of list1, we are confident that the type of the member is int.

This type parameter concept is very powerful, and it’s becoming a basic feature for all modern programming language nowadays. Not just languages on top of .NET platform, .NET itself has integrate generic deeply as part of CLI and CLR implementation.

Other platform such as Java had begun implementation of generic in Java 5 (JDK 1.5.0) although it came late. Luckily, there’s an official tutorial of generics in Java from Oracle.

By integrating generic deeply into the runtime, now it’s up to the programming language to leverage this! In .NET, we have C#, VB, F#, C++.

Hey, it’s time for the participants to try!

WP_000184-2_thumb_71186989

Finally, they could create the LINQ WHERE! …And now time to eat!

WP_000191_thumb_5A38D666

Next meetup: advanced LINQ concept.




Sidenote


Again, I encourage all of you to read TAPL book by Benjamin Pearce, to fully understand the type theory behind generic type.

No comments:

Post a Comment