Herding Nulls and other C# stories from the future

Slides:



Advertisements
Similar presentations
The Spec# programming system K. Rustan M. Leino Microsoft Research, Redmond, WA, USA Lunch seminar, Praxis Bath, UK 6 Dec 2005 joint work with Mike Barnett,
Advertisements

Spec# K. Rustan M. Leino Senior Researcher Programming Languages and Methods Microsoft Research, Redmond, WA, USA Microsoft Research faculty summit, Redmond,
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Mike Barnett RSDE Microsoft Research Nikolai Tillmann RSDE Microsoft Research TL51.
The Spec# programming system K. Rustan M. Leino Microsoft Research, Redmond, WA, USA Distinguished Lecture Series Max Planck Institute for Software Systems.
Equality Programming in C# Equality CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Equality Programming in C# Equality CSE / ECE 668 Prof. Roger Crawfis.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
ECE122 Feb. 22, Any question on Vehicle sample code?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
ITF11006.NET The Basics. Data Types – Value Types – Reference Types Flow Control – Conditional – Loop Miscellaneous – Enumerations – Namespaces Class.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
 Boris Jabes Program Manager Lead Microsoft Corporation TL13.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Run on Windows.NET as system component Run on VM (CLR) Black box compilers Edit in Visual Studio Proprietary Run everywhere Deploy with app Compile.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Classes (Part 1) Lecture 3
5/19/2018 1:01 AM © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN.
Windows Developer Day Fall Creators Update Chris Cortes
16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
2.7 Inheritance Types of inheritance
Future of C#
Get Typed with TypeScript!
Upgrading Your C# Programming Skills to Be a More Effective Developer
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
New Features of C# Kuppurasu Nagaraj Microsoft Connect 2016
Structs.
Customizing your device experience with assigned access
New Features in C# 7.0 Mads Torgersen C# Program Manager
Adaptive Code Umamaheswaran Senior Software Engineer
CS 302 Week 11 Jim Williams, PhD.
CSC 143 Inheritance.
Microsoft Connect /17/2018 5:15 AM
Typescript Programming Languages
Microsoft Build /19/2018 2:06 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
Getting Started With TypeScript
Class Structure 28-Nov-18.
C# Today and Tomorrow Mads Torgersen,
Domain Classes Chapter 9.
What’s new in F# 4.1 Phillip Carter Program Manager.
DotnetConf 12/3/2018 1:48 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE.
Interfaces.
Microsoft Connect /7/ :18 AM
Web Development in Visual Studio 2017
Assignment 7 User Defined Classes Part 2
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Welcome to Azure Notebooks
C++ Productivity Improvements
Microsoft Connect /23/ :38 AM
CS 350 – Software Design Singleton – Chapter 21
What’s Coming to C# William Fuqua. What’s Coming to C# William Fuqua.
Microsoft Build /25/2019 1:55 PM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
Microsoft Ignite NZ October 2016 SKYCITY, Auckland.
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
CIS 199 Final Review.
Java’s Central Casting
5. OOP OOP © 2003 Microsoft.
Microsoft Connect /14/ :11 AM
Presentation transcript:

Herding Nulls and other C# stories from the future Mads Torgersen, C# Lead Designer

Stack Overflow - most popular technologies Microsoft Build 2017 9/14/2018 7:59 AM Stack Overflow - most popular technologies stackoverflow.com/insights/survey/2017#most-popular-technologies © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Stack Overflow - most loved technologies Microsoft Build 2017 9/14/2018 7:59 AM Stack Overflow - most loved technologies stackoverflow.com/insights/survey/2017#most-loved-dreaded-and-wanted © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Herding nulls – the problem Every reference type allows null! Code must be defensive about it C# shares this with most object oriented languages Not all languages have this problem Differentiate between nullable and non-nullable Use pattern matching or similar to conditionally “unpack” nullables Components of a solution Expression of intent (nullable or not?) Enforcement of intent (regulate assignment and dereference) Within an existing language! There are billions of lines of C# code, many of them well-tested against null

Herding nulls – expression of intent Microsoft Build 2017 9/14/2018 7:59 AM Herding nulls – expression of intent public class Person { public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } } © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Herding nulls – expression of intent Microsoft Build 2017 9/14/2018 7:59 AM Herding nulls – expression of intent public class Person { public string! FirstName { get; set; } public string? MiddleName { get; set; } public string! LastName { get; set; } }  © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Herding nulls – expression of intent Microsoft Build 2017 9/14/2018 7:59 AM Herding nulls – expression of intent public class Person { public string FirstName { get; set; } public string? MiddleName { get; set; } public string LastName { get; set; } }  © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Herding nulls – enforcement Protect non-null types from nulls Protect nulls from dereference Must be optional Turn it on when you’re ready to know Can’t affect semantics - warnings, not errors Must do a good job with existing code Can’t force you to rewrite good code Recognize existing ways of ensuring null safety (checks and assignments) Can’t be exhaustive Tradeoff between completeness and convenience

Extension everything – new members Microsoft Build 2017 9/14/2018 7:59 AM Extension everything – new members extension Student extends Person { // static field static Dictionary<Person, Professor> enrollees = new Dictionary<Person, Professor>(); // instance method public void Enroll(Professor supervisor) { enrollees[this] = supervisor; } // instance property public Professor? Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; // static property public static ICollection<Person> Students => enrollees.Keys; // instance constructor public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); } } © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Extension everything – new members Microsoft Build 2017 9/14/2018 7:59 AM Extension everything – new members extension Student extends Person { static Dictionary<Person, Professor> enrollees = new Dictionary<Person, Professor>(); public void Enroll(Professor supervisor) { enrollees[this] = supervisor; } public Professor? Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; public static ICollection<Person> Students => enrollees.Keys; public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); } } Person mads = new Person("Mads Torgersen", tonyHoare); WriteLine(mads.Supervisor); var tonysStudents = from s in Person.Students where s.Supervisor == tonyHoare select s.Name; © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Extension everything – interfaces Microsoft Build 2017 9/14/2018 7:59 AM Extension everything – interfaces extension Student extends Person { static Dictionary<Person, Professor> enrollees = new Dictionary<Person, Professor>(); public void Enroll(Professor supervisor) { enrollees[this] = supervisor; } public Professor? Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; public static ICollection<Person> Students => enrollees.Keys; public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); } } class Professor { … } interface IStudent string Name { get; } Professor? Supervisor { get; } void Enroll(Professor supervisor); © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Extension everything – interfaces Microsoft Build 2017 9/14/2018 7:59 AM Extension everything – interfaces extension Student extends Person : IStudent { static Dictionary<Person, Professor> enrollees = new Dictionary<Person, Professor>(); public void Enroll(Professor supervisor) { enrollees[this] = supervisor; } public Professor? Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; public static ICollection<Person> Students => enrollees.Keys; public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); } } class Professor { … } interface IStudent string Name { get; } Professor? Supervisor { get; } void Enroll(Professor supervisor); © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Extension everything – interfaces as type classes Microsoft Build 2017 9/14/2018 7:59 AM Extension everything – interfaces as type classes interface IGroup<T> { static T Zero { get; } static T operator +(T t1, T t2); } extension IntGroup extends int : IGroup<int> public static int T Zero => 0; public static T AddAll<T>(T[] ts) where T : IGroup<T> T result = T.Zero; foreach (T t in ts) { result += t; } return result; int sixtyThree = AddAll(new [] { 1, 2, 4, 8, 16, 32 }); © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Async streams – getting rid of callbacks Microsoft Build 2017 9/14/2018 7:59 AM Async streams – getting rid of callbacks © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Records class Person(string First, string Last); Microsoft Build 2017 9/14/2018 7:59 AM Records class Person(string First, string Last); class Person : IEquatable<Person> { public string First { get; } public string Last { get; } public Person(string First, string Last) => (this.First, this.Last) = (First, Last); public void Deconstruct(out string First, out string Last) => (First, Last) = (this.First, this.Last); public bool Equals(Person other) => other != null && First == other.First && Last == other.Last; public override bool Equals(object obj) => obj is Person other ? Equals(other) : false; public override int GetHashCode() => GreatHashFunction(First, Last); … } © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Resources github.com/dotnet/csharplang blogs.msdn.microsoft.com/dotnet Microsoft Build 2017 9/14/2018 7:59 AM Resources github.com/dotnet/csharplang blogs.msdn.microsoft.com/dotnet © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.