Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

Similar documents
Java Collection Framework

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

Object Oriented Design (OOD): The Concept

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Why OO programming? want but aren t. Ø What are its components?

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Principles of Programming Languages

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

Josh Bloch Charlie Garrod Darya Melicher

Architecture of so-ware systems

Preliminary ACTL-SLOW Design in the ACS and OPC-UA context. G. Tos? (19/04/2016)

An Interface with Generics

301AA - Advanced Programming [AP-2017]

Parametric polymorphism and Generics

CSE 331 Software Design & Implementation

Agenda. Excep,ons Object oriented Python Library demo: xml rpc

Charlie Garrod Michael Hilton

301AA - Advanced Programming [AP-2017]

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Objec&ves. Packages Collec&ons Generics. Sept 28, 2016 Sprenkle - CSCI209 1

Encapsula)on CMSC 202

Ø Interface methods are public by default

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

Effec%ve So*ware. Lecture 9: JVM - Memory Analysis, Data Structures, Object Alloca=on. David Šišlák

Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Introspec*on and reflexivity

Java's Memory Management

RDD and Strategy Pa.ern

Generic programming POLYMORPHISM 10/25/13

RV: A Run'me Verifica'on Framework for Monitoring, Predic'on and Mining

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

What is the Java Collections Framework?

Programming Languages and Techniques (CIS120)

Array Basics: Outline

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

CSCE 314 Programming Languages

Generics Collection Framework

Algorithms. Produced by. Eamonn de Leastar

CONTAİNERS COLLECTİONS

Sept 26, 2016 Sprenkle - CSCI Documentation is a love letter that you write to your future self. Damian Conway

Design Pa*erns. Philippe Collet. Master 1 IFI Interna3onal h8p://dep3nfo.unice.fr/twiki/bin/view/minfo/soeeng1213. P.

Chapter 6: Structural Design

Generic classes & the Java Collections Framework. *Really* Reusable Code

BBM 102 Introduc0on to Programming II Spring 2014

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

Lecture 4: Build Systems, Tar, Character Strings

Local defini1ons. Func1on mul1ples- of

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Component diagrams. Components Components are model elements that represent independent, interchangeable parts of a system.

301AA - Advanced Programming [AP-2017]

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

CSE Lecture 10: Modules and separate compila5on 18 Feb Nate Nystrom University of Texas at Arlington

Design Principles & Prac4ces

Review. Asser%ons. Some Per%nent Ques%ons. Asser%ons. Page 1. Automated Tes%ng. Path- Based Tes%ng. But s%ll need to look at execu%on results

Java: Past, Present, and Future. Brian Goetz Java Language Architect Oracle Corpora>on

Mo#va#ng the OO Way. COMP 401, Fall 2017 Lecture 05

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Introduc9on to design paderns

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Java Generics. Lecture CS1122 Summer 2008

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Principles of So3ware Construc9on: Objects, Design, and Concurrency. Designing classes

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Outline. Pointers arithme.c and others Func.ons & pointers

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

boolean add(object o) Method Description (from docs API ) Method Description (from docs API )

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

PIC 20A Collections and Data Structures

Model- Based Security Tes3ng with Test Pa9erns

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Sets and Maps. Part of the Collections Framework

Josh Bloch Charlie Garrod Darya Melicher

The plan. Racket will return! Lecture will not recount every single feature of Java. Final project will be wri)ng a Racket interpreter in Java.

Automated System Analysis using Executable SysML Modeling Pa8erns

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Clinical Metadata A complete metadata and project management solu6on. October 2017 Andrew Ndikom and Liang Wang

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Array Basics: Outline

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Principles of Programming Languages

The Java Collections Framework and Lists in Java Parts 1 & 2

Object Oriented Programming. Feb 2015

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Generics method and class definitions which involve type parameters.

Object Model. Object Oriented Programming Spring 2015

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

Generics with Type Bounds

Automa'c Test Genera'on

Programming Languages and Techniques (CIS120)

JCF: user defined collections

Proofs about Programs

Charlie Garrod Bogdan Vasilescu

Structure Patters: Bridge and Flyweight. CSCI 3132 Summer

Transcription:

Genericity Philippe Collet Master 1 IFI Interna3onal 2013-2014 h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314 P. Collet 1

Agenda Introduc3on Principles of parameteriza3on Principles of genericity Genericity in OO languages Sta3c and dynamic genericity Generics in Java 5 P. Collet 2

On sofware evolu3on Mastering evolu3on is one of the major challenges of sofware engineering: maintenance cost, quali/es deteriora/on, oversight, bugs Mastering evolu2on consists in: Introducing needed modifica3ons Only needed ones Wherever this is needed Without impairing ini3al quali3es At the lower cost F Difficult Problem especially if the sofware is large and complex P. Collet 3

On sofware evolu3on (cont d) A lot of sofware techniques aim at facilita3ng evolu3on management: Avoid duplica3on (uniqueness in defini3on) Hide what is not useful (encapsula3on) Locate and explain dependencies Two families of techniques to manage evolu3on: Planned techniques An3cipate possible evolu3ons, to make them easier and safer Adap2ve techniques «lazy» change handling, one at a 3me, but in the most automa3c way P. Collet 4

Planned vs. Adap3ve Planned Parameteriza3on, factoriza3on, genericity, formal models, model- driven engineering Raising the level of abstrac3on, overhead of crea3on should be compensated by ROI Adap3ve Rewri3ng, inheritance, separa3on of concerns, dynamically reconfigurable components Each technique borrows a small part of characteris3cs from the other: There is some planning in inheritance There is some adap3veness in genericity Both techniques complement themselves and can combine with eachother P. Collet 5

Principles of parameteriza3on Define a formal parameter of an en3ty, abstract and general, which displays what is necessary and sufficient Subs3tute in an automa3c way formal by effec3ve parameters, with no impact on the use of the formal ones Verify that: Formal parameters are well used (typing) Effec3ve parameters conform to formal ones Usages of generated en33es are coherent P. Collet 6

Parameteriza3on on values A value is a simple concept : value + type The formal parameter is typed and hides its poten2al value One can use it and verify its type Effec3ve values can be automa3cally subs3tuted on all occurrences in any document Simple subs3tu3on, macrogenera3on, cpp, sed Checking intensity depends on available knowledge: Nothing: macrogenera3on Classic typing: sta3c checking at compila3on 3me, or at binding and run 3mes Asser3ons, formal specifica3ons: dynamic checking, proofs P. Collet 7

Principles of genericity A lot of things are generic: medicines In programming, genericity is a form of type genericity In languages: Impera3ve (pioneer): CLU, LPG, Euclide, Ada OO: Eiffel, then C++, C#, Java This is always a constant search for tradeoffs between: Flexibility in deriva3ons Checking safety Cost at compila3on and run 3mes Simplicity, readability P. Collet 8

Generic OO languages Eiffel (pioneer) Powerful and complex mul3ple inheritance Primi3ve types as expanded objects (no limita3ons for genericity) No genericity informa3on at run3me (a simple technique of sta3c typing) Genericity very readable and very simple Constrained genericity through abstract classes Automa3c constraints with like object / like current No introspec3on on effec3ve types P. Collet 9

Generic OO languages (2) C++ Macro- genera3on at compila3on or linking 3mes Checking done through the linkers => error messages are strange No constrained genericity (implicit genericity in signatures) No guarantee that the introspec3on of effec3ve types will be possible at run3me P. Collet 10

Generic OO languages (3) C# Instan3a3on of generic types at run/me Java Genericity constrainted by class and interface Type checking by the compiler and possible introspec3on at run3me Ten years to put chunks of genericity inside Compa3bility at the code level between generic/non- generic sofware Very sophis3cated, but complex Inconsistencies and performance loss due to compability constraints (cast, type erasure ) Some problems with primi3ve types and arrays P. Collet 11

Principles of dynamic genericity An object carries informa3on on its proper3es (through reflexivity) and on its co- instances proper3es The technique is similar to the prototype design pa9ern The formal parameter is an a9ribute of type T All proper3es of T can be used in the model Subs3tu3on is dynamic, by a simple (polymorphic) assignment Possible checking: by dynamic reflexivity, very flexible but costly P. Collet 12

Example: polymorphic vector Addi3on of two polymorphic vectors: V1 + V2 = V3 5 10 15 2.007 Ali 3.14 5.147 Baba Ali Baba true false true Advantages: Get more flexibility than with sta3c typing (with which all elements have the same type) No concession on rigorous typing While doing dynamic checking P. Collet 13

Solu3on #1 : No sta3c checking All elements are of type Object One checks by introspec3on that two elements of the same index are compa3ble for the addi3on The method «plus» of the first element is used to add the second one This is possible in all OO languages with introspec3on P. Collet 14

Solu3on #2: mixed checking, sta3c and dynamic The necessary and sufficient sta3c typing is added to the previous solu3on: All elements are of type Monoid class PolymorphicVector <Monoid> extends ArrayList<Monoid> { } P. Collet 15

Solu3on #2: mixed checking, sta3c and dynamic (cont d) One can sta3cally constrain: PolymorphicVector <Number> One can dynamically constrain: class DynamicMonoVector extends ArrayList<Monoid> implements Monoid { Monoid prototype ; // fournit le + Void DynamicMonoVector(Monoid type){ super(); prototype = type; } // Usage DynamicMonoVector pvi = new DynamicMonoVector(new Integer(0)); P. Collet 16

Other possibili3es By combining sta3c and dynamic approaches, one has various possibili3es to choose: The degree of polymorphism: all of the same type, same sur- type, same type two by two When the constraint is defined and checked : at compile or run 3mes P. Collet 17

Generics in Java 5 The Collec3on Class : Public interface Collection<E> extends Iterable<E> { int size(); boolean isempty(); boolean contains(object o); Iterator<E> iterator(); boolean add(e o); boolean remove(object o); boolean containsall(collection<?> c); boolean addall(collection<? extends E> c); boolean removeall(collection<?> c); boolean retainall(collection<?> c); void clear(); boolean equals(object o); int hashcode(); } P. Collet 18

Java 5: generic parameter Declara3on of a formal parameter that will be used throughout the class Use the nota3on <T> just afer the class name Use T in the rest of the code Conven3on: use a capital le9er for the parameter id T will be replaced by a given class at instan3a3on 3me Genericity can be limiter to a specific method: Use the nota3on <T> just before the method signature Use T in the rest of the method code T will be infered from the method argument types P. Collet 19

Kinds of generic declara3ons Novariance = Covariance Contravariance : only the specified class is accepted <A> a class A Bivariance = Covariance Contravariance : All classes are accepted <?> joker/unknown any class Covariance (all sub- classes are acceptable) <A extends B> any class A that specializes B (extends, implements) <? extends B> any class that specializes B (extends, implements) Contravariance (all super- classes are acceptable) <? super B> any class that generalizes B P. Collet 20

Illustra3ons // Novariance List <Integer> l = new ArrayList <Integer>(); // compilation error: default is novariance // List <Number> l1 = l; // possible : covariance List <? extends Number> l1 = l; // bivariance : typing is lost List <?> l2 = new ArrayList <Object>(); // contravariance List <? super Number> l3 = new ArrayList <Object>(); for (Number n : l1) { // compilation error: l2 could have been a list of Double! // l2.add(n); l3.add(n); // OK } P. Collet 21

Illustra3ons (2) // Covariance and contravariance public static <T> void copy(collection <? extends T> src, Collection <? super T> dest) { for(t t : src) { dest.add(t); } } List <Integer> l1 = new ArrayList <Integer>(); for(int i=0; i<10; i++) { l1.add(i); } List <Number> l2 = new ArrayList <Number>(); copy(l1,l2); copy (l2,l1); // Compilation error // Number(s) cannot be put in a list of Integer P. Collet 22

Survival guide: PECS principle PECS: Producer extends, Consumer super Use Foo<? extends T> for a producer of T Use Foo<? super T> for a consumer of T Only applicable to method parameters No joker in return types P. Collet 23

PECS : applica3on public static <T> void copy(collection<t> src, Collection<T> dest) In copy: src provides elements of type T dest consumes elements of type T public static <T> void copy( Collection <? extends T> src, Collection <? super T> dest) P. Collet 24

PECS : applica3on (2) public static <E> Set<E> union(set<e> s1, Set<E> s2) In union: s1 and s2 are producers of elements E! public static <E> Set<E> union(set<? extends E> s1, Set<? extends E> s2) P. Collet 25

Type erasure The bytecode understood by a JVM v5 does not contain any trace of genericity due to the constraint of backward compa3bility A Java 5+ compiler: Replace parameters by Object (case of novariance or bivariance) or by the bounded type (co/contra- variance) Create methods with compa3ble signatures for the other cases The problem of return types is solved by the covariance P. Collet 26

Conclusions on genericity Genericity allows for factoriza3on and reuse of knowledge and know- how with guaranteed quality Can everything be made generic? NO! The reuse of architectures or canvas to solve a general problem cannot be, most ofen, described by a generic unit. Solu/ons: Design pa9erns Frameworks Domain Specific Languages Dedicated systems (DB, IDE ) SoFware Product Lines P. Collet 27