CSC/ECE 517 Fall 2012/ch1b 1w47 sk

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

Generic Programming is a programming paradigm for developing efficient and reusable software libraries. The Generic Programming process focuses on finding commonality among similar implementations of the same algorithm, then providing suitable abstractions so that a single, generic algorithm can cover many concrete implementations. This process is repeated until the generic algorithm has reached a suitable level of abstraction, where it provides maximal re-usability while still yielding efficient, concrete implementations.

In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by Ada in 1983, permits writing common functions or types that differ only in the set of types on which they operate on, thus reducing duplication. Such software entities are known as generics in Ada, Eiffel, Java and .NET; parametric polymorphism in ML, Scala and Haskell ; templates in C++; and parameterized types in the influential 1994 book, Design Patterns.

The term generic programming was originally coined by David Musser and Alexander Stepanov in a more specific sense than the above, to describe an approach to software decomposition whereby fundamental requirements on types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts, analogously to the abstraction of algebraic theories in abstract algebra. Early examples of this programming approach were implemented in Scheme and Ada, although the best known example is the Standard Template Library (STL) in which is developed a theory of iterators which is used to decouple sequence data structures and the algorithms operating on them.

Generics in Object Oriented Languages

Generics .NET framework

Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. A generic collection class might use a type parameter as a placeholder for the type of objects that it stores; the type parameters appear as the types of its fields and the parameter types of its methods. A generic method might use its type parameter as the type of its return value or as the type of one of its formal parameters.

The following code illustrates a simple generic class definition in C#.

public class Generic<T>
{
    public T Field;
}

The following code illustrates a simple generic class definition in Visual Basic.

Public Class Generic(Of T)
    Public Field As T
End Class

When an instance of a generic class is created, one can specify the actual types to substitute for the type parameters. This establishes a new generic class, referred to as a constructed generic class, with the chosen types substituted everywhere that the type parameters appear. The result is a type-safe class that is tailored to the user's choice of types, which is illustrated in the code below in C# and VB.

//Code in C#
public static void Main()
{
    Generic<string> g = new Generic<string>();
    g.Field = "A string";
}
'Code in Visual Basic
Public Shared Sub Main()
    Dim g As New Generic(Of String)
    g.Field = "A string" 
End Sub

Generics Terminology in .NET

  • A generic type definition is a class, structure, or interface declaration that functions as a template, with placeholders for the types that it can contain or use. For example, the Dictionary class can contain two types: keys and values. Because a generic type definition is only a template, so one cannot create instances of a class, structure, or interface that is a generic type definition.
  • Generic type parameters, or type parameters, are the placeholders in a generic type or method definition. The Dictionary generic type has two type parameters, TKey and TValue, that represent the types of its keys and values.
  • A constructed generic type, or constructed type, is the result of specifying types for the generic type parameters of a generic type definition.
  • A generic type argument is any type that is substituted for a generic type parameter.
  • The general term generic type includes both constructed types and generic type definitions.
  • Covariance and contravariance of generic type parameters enable one to use constructed generic types whose type arguments are more derived (covariance) or less derived (contravariance) than a target constructed type. Covariance and contravariance are collectively referred to as variance.
  • Constraints are limits placed on generic type parameters. For example, one might limit a type parameter to types that implement the IComparer generic interface, to ensure that instances of the type can be ordered. One can also constrain type parameters to types that have a particular base class, that have a default constructor, or that are reference types or value types. Users of the generic type cannot substitute type arguments that do not satisfy the constraints.
  • A generic method definition is a method with two parameter lists: a list of generic type parameters and a list of formal parameters. Type parameters can appear as the return type or as the types of the formal parameters. The following code shows this in C# and Visual Basic.
//Code in C#
T Generic<T>(T arg)
{
    T temp = arg;
    //... 
    return temp;
}
'Code in Visual Basic
Function Generic(Of T)(ByVal arg As T) As T
    Dim temp As T = arg
    '... 
    Return temp
End Function
  • Generic methods can appear on generic or nongeneric types. A method is not generic just because it belongs to a generic type, or even because it has formal parameters whose types are the generic parameters of the enclosing type. A method is generic only if it has its own list of type parameters. In the following code, only method G is generic.
//Code in C#
class A
{
    T G<T>(T arg)
    {
        T temp = arg;
        //... 
        return temp;
    }
}
class Generic<T>
{
    T M(T arg)
    {
        T temp = arg;
        //... 
        return temp;
    }
}
'Code in Visual Basic
Class A
    Function G(Of T)(ByVal arg As T) As T
        Dim temp As T = arg
        '... 
        Return temp
    End Function 
End Class 
Class Generic(Of T)
    Function M(ByVal arg As T) As T
        Dim temp As T = arg
        '... 
        Return temp
    End Function 
End Class

Nested Types and Generics

A type that is nested in a generic type can depend on the type parameters of the enclosing generic type. The common language runtime considers nested types to be generic, even if they do not have generic type parameters of their own. When you create an instance of a nested type, you must specify type arguments for all enclosing generic types.

Language Support

The .NET Framework provides a number of generic collection classes in the following namespaces:

  • The System.Collections.Generic namespace catalogs most of the generic collection types provided by the .NET Framework, such as the List and Dictionary generic classes.
  • The System.Collections.ObjectModel namespace catalogs additional generic collection types, such as the ReadOnlyCollection generic class, that are useful for exposing object models to users of your classes.
  • Generic interfaces for implementing sort and equality comparisons are provided in the System namespace, along with generic delegate types for event handlers, conversions, and search predicates.
  • The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL), including Stelem, Ldelem, Unbox_Any, Constrained, and Readonly.
  • Visual C++, C#, and Visual Basic all provide full support for defining and using generics.


Generics in Java

Generics were introduced in Java from JDK 1.5. Generics allows the abstraction over types. The most common examples are container types, such as those in the Collection hierarchy. So, if one could say that the code works with some unspecified type, rather than a specific interface or class, then the code uses Generics. Below is an example of a Genric:

List myIntList = new LinkedList(); 
myIntList.add(new Integer(0)); 
Integer x = (Integer) myIntList.iterator().next(); 

Typically, a programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter, but also introduces the possibility of a run time error, since the programmer might be mistaken. It would be better if programmers could actually express their intent, and mark a list as being restricted to contain a particular data type. This is the core idea behind generics.

Here is a variation of the program fragment given above using generics:

List<Integer> myIntList = new LinkedList<Integer>();
myIntList.add(new Integer(0)); 
Integer x = myIntList.iterator().next();

The type declaration for the variable myIntList specifies that this is not just an arbitrary List, but a List of Integer, written List<Integer>. The List is a generic interface that takes a type parameter - in this case, Integer. One has to specify a type parameter when creating the list object. The compiler can now check the type correctness of the program at compile-time. Because myIntList is declared with type List<Integer>, this indicates that myIntList holds a list of integers, which holds true wherever and whenever it is used, and the compiler ensures to guarantee it. In contrast, the cast tells something the programmer thinks is true at a single point in the code. The net effect, especially in large programs, is improved readability and robustness.

Below is code for generic interfaces List and Iterator in package java.util.The content in angle brackets are the declarations of the formal type parameters of the interfaces List and Iterator.

public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
public interface Iterator<E> {
E next();
boolean hasNext();
}

Generics and Subtyping

Below is an example showing code for Generics and Subtyping.

List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2
lo.add(new Object()); //3
String s = ls.get(0); //4 attempts to assign an Object to a String

Line 1 is legal. Lines 2,3 and 4 aliased ls and lo. Accessing ls, a list of String, through the alias lo, one can insert arbitrary objects into it. As a result ls does not hold just Strings anymore. The Java compiler will prevent this from happening and Line 2 causes a compile time error.In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>.

Wildcards

Consider the problem of writing a routine that prints out all the elements in a collection. In older version's of Java, it can be written as below:

void printCollection(Collection c) {
   Iterator i = c.iterator();
   for (k = 0; k < c.size(); k++) {
       System.out.println(i.next());
   }
}

In the later versions of Java, the same logic above can be written using generics using the syntax of enhanced for loop:

void printCollection(Collection<Object> c) {
   for (Object e : c) {
      System.out.println(e);
   }
}

The old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which is not a supertype of all kinds of collections. The supertype of all kinds of collections is written Collection<?>, that is, a collection whose element type matches anything. It’s called a wildcard type. Below is the code expressing the same functionality using a wildcard.

void printCollection(Collection<?> c) {
   for (Object e : c) {
      System.out.println(e);
   }
}

The above code can be called with any type of collection. Inside printCollection(), one can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. However, It is not safe to add arbitrary objects to it.

Generic Methods

Generic methods are used more often than generic classes. A generic class is independent of whether you have a generic method. If a method is static, it has no access to the generic type parameters of the class, so if it needs to use genericity it must be a generic method. Unlike generic class one don’t usually have to specify the parameter types, because the compiler can figure that out.

public class Gen {
   public <T> void f(T x) {
       System.out.println(x.getClass().getName());
   }

   public static void main(String[] args) {
       Gen a = new Gen();
       a.f("");
       a.f(‘c’);
   }
}

Erasure

To implement generics, the Java compiler applies type erasure to:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • Insert type casts if necessary to preserve type safety.
  • Generate bridge methods to preserve polymorphism in extended generic types.

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

Consider the example below.

public class A {
   public static void main(String[] args) {
       System.out.println(new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass());
   }
}

Here ArrayList<String> and ArrayList<Integer> seem to be of different types and so one expects them to behave differently. But the execution of the above program returns true which suggests they are of the same type.

class Fn {}
class Foo<Q> {}
class Boo<X,Y> {}

public class AbsentInformation {
   public static void main(String[] args) {
      Foo<Fn> foo = new Foo<Fn>();
      Boo<Long,Double> b = new Boo<Long,Double>();

      System.out.println(Arrays.toString(foo.getClass().getTypeParameters()));
      System.out.println(Arrays.toString(b.getClass().getTypeParameters()));
   }
} 
Output:
[Q]
[X,Y]

Class.getTypeParameters() suggest that one might be able to find out what the parameter types are. But in reality, the identifiers that are used as the parameter placeholders. This is one important feature where Java differs from C++. Java generics are implemented using erasure. So when one uses generics, the specific information is erased. The only thing one knows is that we are using an object. So both List<String> and List< Integer> have the same type at runtime.

Generics in C++

Advantages and Disadvantages

These actually generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type.

While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem.

Comparing C# and Java Generics

Java's generics implementation was based on a project originally called Pizza, which was done by Martin Odersky and others. Pizza was renamed GJ, then it turned into a JSR and ended up being adopted into the Java language. And this particular generics proposal had as a key design goal that it could run on an unmodified VM [Virtual Machine]. It is, of course, great that you don't have to modify your VM, but it also brings about a whole bunch of odd limitations. The limitations are not necessarily directly apparent, but you very quickly go, "Hmm, that's strange."

For example, with Java generics, you don't actually get any of the execution efficiency that I talked about, because when you compile a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere. So the compiled image for List<T> is like a List where you use the type Object everywhere. Of course, if you now try to make a List<int>, you get boxing of all the ints. So there's a bunch of overhead there. Furthermore, to keep the VM happy, the compiler actually has to insert all of the type casts you didn't write. If it's a List of Object and you're trying to treat those Objects as Customers, at some point the Objects must be cast to Customers to keep the verifier happy. And really all they're doing in their implementation is automatically inserting those type casts for you. So you get the syntactic sugar, or some of it at least, but you don't get any of the execution efficiency. So that's issue number one I have with Java's solution.

Issue number two, and I think this is probably an even bigger issue, is that because Java's generics implementation relies on erasure of the type parameter, when you get to runtime, you don't actually have a faithful representation of what you had at compile time. When you apply reflection to a generic List in Java, you can't tell what the List is a List of. It's just a List. Because you've lost the type information, any type of dynamic code-generation scenario, or reflection-based scenario, simply doesn't work. If there's one trend that's pretty clear to me, it's that there's more and more of that. And it just doesn't work, because you've lost the type information. Whereas in our implementation, all of that information is available. You can use reflection to get the System.Type for object List<T>. You cannot actually create an instance of it yet, because you don't know what T is. But then you can use reflection to get the System.Type for int. You can then ask reflection to please put these two together and create a List<int>, and you get another System.Type for List<int>. So representationally, anything you can do at compile time you can also do at runtime.

Comparing C# Generics to C++ Templates

To me the best way to understand the distinction between C# generics and C++ templates is this: C# generics are really just like classes, except they have a type parameter. C++ templates are really just like macros, except they look like classes.

The big difference between C# generics and C++ templates shows up in when the type checking occurs and how the instantiation occurs. First of all, C# does the instantiation at runtime. C++ does it at compile time, or perhaps at link time. But regardless, the instantiation happens in C++ before the program runs. That's difference number one. Difference number two is C# does strong type checking when you compile the generic type. For an unconstrained type parameter, like List<T>, the only methods available on values of type T are those that are found on type Object, because those are the only methods we can generally guarantee will exist. So in C# generics, we guarantee that any operation you do on a type parameter will succeed.

C++ is the opposite. In C++, you can do anything you damn well please on a variable of a type parameter type. But then once you instantiate it, it may not work, and you'll get some cryptic error messages. For example, if you have a type parameter T, and variables x and y of type T, and you say x + y, well you had better have an operator+ defined for + of two Ts, or you'll get some cryptic error message. So in a sense, C++ templates are actually untyped, or loosely typed. Whereas C# generics are strongly typed.

References

1. http://www.artima.com/intv/genericsP.html
2. http://www.generic-programming.org
3. Generics in the Java Programming Language
4. http://www.cplusplus.com/doc/tutorial/templates
5. http://osl.iu.edu/publications/prints/2003/comparing_generic_programming03.pdf
6. http://msdn.microsoft.com/en-us/library/ms172192.aspx#Y1524
7. http://www.justsoftwaresolutions.co.uk/articles/intrototemplates.pdf
8. http://www.sgi.com/tech/stl/stl_introduction.html