Variance. Svetlana Isakova

Similar documents
Mixed projects: Java + Kotlin. Svetlana Isakova

The Kotlin Programming Language. Andrey Breslav

The Kotlin Programming Language. Andrey Breslav Dmitry Jemerov

CSCE 314 Programming Languages

Exercise 8 Parametric polymorphism November 17, 2017

21 Subtyping and Parameterized Types

Exercise 8 Parametric polymorphism November 18, 2016

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Taming the Wildcards

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

Refactoring Java Generics by Inferring Wildcards, In Practice

CSE 331 Software Design & Implementation

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

Writing Java-friendly Kotlin code. Sergey Ryabov

Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

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

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

A Deep Dive into the Void

Copyright 2018 Tendril, Inc. All rights reserved.

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

301AA - Advanced Programming [AP-2017]

Lecture 13: Subtyping

The Hack programming language:

301AA - Advanced Programming [AP-2017]

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Polymorphism (generics)

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

CSE 331 Software Design & Implementation

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Subtyping and Objects

Collections, Maps and Generics

Comp215: Covariance and Contravariance

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

GETTING STARTED WITH Kotlin INTRODUCTION WHERE TO START CODING BASIC SYNTAX

CMSC 202. Containers

Java Generics -- an introduction. Based on

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CSCI-GA Final Exam

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Java: exceptions and genericity

Kotlin for Android developers

Kotlin, Start? Start! (pluu) Android Developer GDG Korea Android Organizer

What is Inheritance?

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

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

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

Adventures on the Road to Valhalla

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

The DOT Calculus. (Dependent Object Types) Nada Amin. June 18, Scala Days

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

LAMBDA EXPRESSIONS AND STREAMS API

A Deep Dive Into Kotlin

Generics: Past, Present and Future

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Outline. 1 Generic Classes. 2 Generics and Subtyping. Generics. Guy Wiener. Generic Classes. Generics and Subtyping

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

Parametric polymorphism and Generics

The DOT Calculus. (Dependent Object Types) Nada Amin. May 12, flatmap(oslo)

CIS 341 Final Examination 4 May 2017

Lecture 6: Generics. Lisa (Ling) Liu

CSE 331 Software Design & Implementation

Generics with Type Bounds

Java 5 New Language Features

Refactoring Java Generics by Inferring Wildcards, In Practice

COMP 401 Spring 2014 Midterm 1

Implementing Method Type Specialisation In Dotty

Javarifier: inference of reference immutability

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Advanced Object-Oriented Languages Scala

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

Peter Sestoft. Java Precisely. Third Edition. The MIT Press Cambridge, Massachusetts London, England

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

Practice Questions for Chapter 9

Subtyping for behaviour?

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Generics: Past, Present

Subtyping (cont) Lecture 15 CS 565 4/3/08

CSE 331. Generics (Parametric Polymorphism)

Homework 9 CSCI 334: Spring, 2019

Java Design Goals. Lecture 32: Java. Java Original implementations slow! Exceptions & Subtyping. - void method readfiles() throws IOException {...}!

Subtyping for behaviour?

Primitive Java Generic Class

Photo credit: Andrew Kennedy JAVA GENERICS

Generic Universe Types in JML

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Transcription:

Variance Svetlana Isakova

Variance Describes how types with the same base type and different type arguments relate to each other Animal Producer<Animal> Consumer<Animal> Cat Producer<Cat> Consumer<Cat>

Example: Java arrays Is it possible in Java to pass String[] as an argument for Object[] parameter? yes Object[] Is it safe? no String[]

java.lang.arraystoreexception String[] strings = foo(strings); new String[] { "a", "b", "c" ; private static void foo(object[] array) { /*... */

java.lang.arraystoreexception String[] strings = foo(strings); new String[] { "a", "b", "c" ; private static void foo(object[] array) { array[1] = 42; java.lang.arraystoreexception: java.lang.integer

Java: lists are safe List<String> strings = Arrays.asList("a", "b", "c"); foo(strings); Error: incompatible types: java.util.list<java.lang.string> cannot be converted to java.util.list<java.lang.object> private static void foo(list<object> list) { list.set(0, 42);

Definitions

Classes & types Classes Types String String String? List List<Int> List<String?> List<List<String>>

Subtyping A type B is a subtype of a type A if you can use the value of the type B whenever a value of the type A is required. A Number Int String B Int Int Int

The compiler always checks subtyping fun test(i: Int) { val n: Number = i fun f(s: String) { /*...*/ f(i) Error: inferred type is Int but String was expected

Subtyping for nullable types val s: String = "abc" val t: String? = s A? Int? Int A Int Int?

Subtyping for generic types interface Foo<T> { A Foo<A>? B Foo<B>

Declaration-site variance

Invariant classes A generic class Foo is called invariant on the type parameter if, for any two different types A and B, Foo<A> is not a subtype or a supertype of Foo<B> A Foo<A> Foo<B> B Foo<B> Foo<A>

Covariance A generic class Producer<T> is covariant on its type parameter if the following holds: Producer<B> is a subtype of Producer<A> when B is a subtype of A. A Producer<A> B Producer<B>

List & MutableList Any MutableList<Any> List<Any> String MutableList<String> List<String>

Kotlin: mutable lists val list = mutablelistof("a", "b", "c") foo(list) Error: Type mismatch: inferred type is MutableList<String> but MutableList<Any> was expected fun foo(list: MutableList<Any>) { list += 42

Kotlin: read-only lists val list = listof("a", "b", "c") foo(list) fun foo(list: List<Any>) { list += 42

Declaration-site variance

in / out positions interface Transformer<T, R> { fun transform(t: T): R in position out position

<out T> means covariant interface Producer<out T> { fun produce(): T T must be used Producer<Any> only in out positions Producer<Int>

Why is it safe? interface Producer<out T> { fun produce(): T val producer: Producer<Int> foo(producer) fun foo(anyproducer: Producer<Any>) { val any = anyproducer.produce()

<T> means invariant interface Foo<T> { fun bar(): T fun baz(t: T) Foo can t be declared as covariant, because T is used both in out and in positions

List<out T> interface List<out T> : Collection<T> { operator fun get(index: Int): T fun sublist(fromindex: Int, //... toindex: Int): List<T> interface MutableList<T> : List<T>, MutableCollection<T> { override fun add(element: T): Boolean

@UnsafeVariance interface List<out T> : Collection<T> { operator fun get(index: Int): T override fun contains( element: @UnsafeVariance T): Boolean //...

Contravariance

Contravariance A generic class Consumer<T> is contravariant on its type parameter if the following holds: Consumer<A> is a subtype of Consumer<B> when B is a subtype of A. A Consumer<A> B Consumer<B>

<in T> means contravariant interface Consumer<in T> { fun consume(t: T) T must be used Consumer<Any> only in in positions Consumer<Int>

Example: comparator interface Comparator<in T> { fun compare(o1: T, o2: T): Int Any Comparator<Any> Int Comparator<Int>

Using comparator val anycomparator: Comparator<Any> = Comparator { a, b -> a.hashcode() - b.hashcode() fun foo(strcomparator: Comparator<String>) { listof("a", "c", b"). sortedwith(strcomparator)

Covariant, contravariant & invariant classes Covariant Contravariant Invariant Producer<out T> Consumer<in T> Foo<T> T only in "out" positions T only in "in" positions T in any position Producer<Any> Producer<Int> Consumer<Any> Consumer<Int> Foo<Any> Foo<Int>

(T) -> R interface Function1<in P, out R> { operator fun invoke(p: P): R Cat (Cat) -> Number Number Animal (Animal) -> Int Int

Use-site variance

Java wildcards public interface Stream<T> { <R> Stream<R> map( Function<? super T,? extends R> mapper);

copydata example fun <T> copydata( source: MutableList<T>, destination: MutableList<T>) { for (item in source) { destination.add(item) val ints = mutablelistof(1, 2, 3) val anyitems = mutablelistof<any>() copydata(ints, anyitems) Error: Cannot infer type parameter T

First solution: one more type parameter fun <T : R, R> copydata( source: MutableList<T>, destination: MutableList<R>) { for (item in source) { destination.add(item) val ints = mutablelistof(1, 2, 3) val anyitems = mutablelistof<any>() copydata(ints, anyitems)

Another solution: MutableList<out T> fun <T> copydata( source: MutableList<out T>, destination: MutableList<T>) { for (item in source) { destination.add(item)

Projected type val list: MutableList<out Number> = list.add(42) Error: Out-projected type 'MutableList<out Number>' prohibits the use of 'fun add(element: E): Boolean'

MutableList<in T> fun <T> copydata( source: MutableList<T>, destination: MutableList<in T>) { for (item in source) { destination.add(item)

Star projection MutableList<*> MutableList<Any?> List<*> = List<out Any?> = List<Any?>

Using List<*> fun printfirst(list: List<*>) { if (list.isnotempty()) { println(list.first()) returns Any?

Star projection MutableList<*> MutableList<Any?> MutableList<*> = MutableList<out Any?> = Comparator<*> Comparator<in Nothing>???>

Thank you!