Skip to content

zoufab/pecs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PECS

Producer Extends Cosumer Super

1. Covariance

? extends MyClass

MyClass is the highest in hierarchy of inheritance

"I can get it from the wildcard type ?"

"I can get values out of a structure."

2. ContraVariance

? super MyClass

MyClass is the lowest in the hierarchy of inheritance.

"I can give it to the wildcard type ?"

"I can put values into a structure."

3. NonVariance/Variance

MyClass

Examples

abstract A {}

class B extends A {}

class C extends B {}


public class Pecs{

	public void testCovariance(List<? extends B> elements){
		B b = new B();
		C c = new C();
		A a = elements.get(0); // PASS: We know th return type, necessarily a subtype of B
		
		elements.add(b); // Exception : ambiguous type given to the structure, include b
		elements.add(c); // Exception
	}


	public void testContraVariance(List<? super B> elements){
		B b = new B();
		C c = new C();
		elements.add(b); // PASS : We know the super of the element's type given to structure, necessarily B at least
		elements.add(c); // PASS
		
		A a = elements.get(0); // Exception : ambiguous type out of structure
		Object a = elements.get(0);
	}

}

Subtype and wildcards

super : data in

extends : data out

Reflection and Raw types

Generic information can't reflect because it gets erased at compile time Raw types represent a type without a parameter MaClass.class

Lambdas and Intersection types

  • Lambda: look at the target type, have their types inferred, use funtionnal interface
  • Intersection types : <T extends A & B> is a powerful system T extends Object & Comparable<? super T>

About

Producer Extends Cosumer Super

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages