• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics in Tiger

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are my notes about Generics in Tiger:
Thanks to the article by Gilad Bracha


1- Gereric classes (Interfaces):

Definition:

class A<E [extends Base]> {
// use E
}

Usage:

A<Type> x = new A<Type>(...);
Where Type is a class name or a type parameter (can't be a primitive)
A x = new A(), A<Type> x = new A(), A x = new A<Type>() work but may gererate a warning and
a ClassCastException (for compatibility with legacy code)

2- A<Derived> is not a subtype of A<Base>.

3- A<?> is the supertype of any A<Type>
It enables accessing A's variables and calling methods whose parameters are not parameterized.
A's Methods with parameterized-type parameters can be called with null.
new A<?>(...); is illegal

4- A<? extends Base> is the supertype of all A<Derived>

5- A<? super Derived> accepts supers of derived

6- A class may have many type parameters: class A<E1, E2>{}

7- Generic Methods:

modifier* <Type-Parameter [extends Type], ...> return-type name (...) {}

When calling the methods, the parameters (should) determine the types.

[extends Type] restrict the type parameter to a subclass of Type

A type parameter can be used in the return type, in the methods parameters and in the body...

8- class type-parameters can't be accessed from a static context.

9- A static member i in A<E> are accessed using A.i (not A<...>.i)

10- (a instanceof A<E> ) is illegal but (a instanceof A)

11- ( A<E> ) a or (T) a --> warning where T is a type variable

12- new A<E>[1], new T[1] are illegal where T is a type variable or a parameterized type

13- A<E>[] x = new A<?>[1] --> warning when using x[i],
A<?>[] x = new A<?>[1] --> Explicit cast required to use x[i]

14- Wildcard capture: <T> Set<T> f(Set<T> set) can be called on Set<?> s = new HashSet<String>()

15- Multitype bound <T extends T1 & T2 & ... & Tn) --> erasure is T1.

16- The class Class<E> is generic

17- covariant returns...

18- A subclass of a generic class Base<A> may be
1- not generic:
class Derived B extends Base {}
Here Base<String> a = new Derived(); --> warning
2- a new Generic:
class Derived B<E> extends Base<T> {}
3- a derived Generic:
class Derived B<E> extends Base<ُE> {}
This is the most safer one

[ January 28, 2005: Message edited by: Bilal Al-Sallakh ]
[ January 29, 2005: Message edited by: Bilal Al-Sallakh ]
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very helpful. thank you _
 
reply
    Bookmark Topic Watch Topic
  • New Topic