SlideShare une entreprise Scribd logo
1  sur  41
Designing nullable
reference types in F#
PHILLIP CARTER - @_CARTERMP
NDC LONDON – JANUARY 2019
Overview
Contextualizing nullability
Design overview
Metadata representation
Nullability assumptions
Adjusting semantics of existing F# code
Why do this feature?
Contextualizing nullability
WHAT’S ALL THE FUSS ABOUT?
Null itself is perfectly valid
It really is!
You need to represent ”nothing” in a programming language
null accomplishes the task of representing “nothing”
Implicit null is the problem
Why no warning?
Mitigations: Options
Mitigations: explicit mutability
Mitigations: non-null type declarations
Problems: AllowNullLiteral attribute
I just needed nullability here
I got nullability everywhere instead
Problems: CLIMutable
Can be made null in an
application’s lifetime!
Problems: General .NET interop
Null?
Null?
Design overview
DUE TO TIME CONSTRAINTS, THIS IS NOT COMPREHENSIVE
Main goals
Distinguish between nullable and non-nullable reference types in the F# type system
Make nullable reference types less desirable to use than non-nullable reference types
Offer helper functionality and patterns for dealing with nulls
Offer compiler analysis for unsafe access of nullable reference types
Syntax and basic examples
More syntax and basic examples
Helper functions and patterns
Generic constraints
‘T when ‘T: null
‘T when ‘T: struct
‘T when ‘T: not struct
‘T when ‘T: not null
New!
Semantics:
• Error if the constraint is instantiated with an
F# type that has null as a proper value
• Warning if constraint is instantiated with a
nullable reference type
Pattern matching (majority case)
NonNull pattern can be used
anywhere, not just at the end
‘null’ pattern MUST be the first
for this ‘s’ to be non-null, as
per existing rules
Pattern matching (column-based)
s1 and s2 are still nullable!
Typechecking proceeds before
column analysis
May need to use NonNull
patterns to handle this
F# type relations with nullability
Type equivalence (x = y; y <> z)
◦ Nullness ignored, but warning given on mismatch
Type subsumption (B a subtype of A)
◦ Nullness ignored, but warning given on mismatch
Method overload resolution
◦ Nullness ignored, but warning given on mismatches in arguments and return types
Abstract slot inference
◦ Nullness ignored, but warning given on slot mismatches
Duplicate method checking
◦ Nullness is completely ignored
Type inference and mixing null/non-null
• First element in each collection is a non-null string
• Introduction of nullable string introduces type equivalence mismatch
• Solution is to add explicit type annotation
Type inference and nullability assertions
We cannot associate any arbitrary bool value with
nullability. A nullability assertion is needed.
Nullability and obj
Nullability is completely ignored for ‘obj’ type
The ‘obj’ type is used when type information is thrown out
Making this nullable/non-nullable is pointless considering throwing out type information
Null analysis for nullable value types
Partial alignment of existing nullable value types is possible
◦ Same syntax may be possible (int?, float?, etc.)
◦ Same or similar rules for type relations and type inference
Metadata
HOW ALL THE INFORMATION GETS ENCODED
[<Nullable>] attribute
bool[] represents nested types
Attributes to affect compiler analysis
[<NotNullWhenTrue>] and [<NotNullWhenFalse>]
◦ Used in bool-returning functions or methods that check for null
[<EnsuresNotNull>]
◦ Used on functions or methods that throw an exception when null is encountered
[<AssertsTrue>] and [<AssertsFalse>]
◦ Used for asserting nullability, such as in test frameworks, and avoiding warnings
[<NonNullTypes>] attribute
Three cases:
• NonNullTypes(true)
• Nullability is a concept
• NonNullTypes(false)
• Nullability is not a concept
• No attribute at all
• Null oblivious semantics
Nullability assumptions
EVERYONE LIKES ASSUMPTIONS
Three choices involved
Distinguish between nullable and non-nullable reference types
Do not distinguish between nullable and non-nullable reference types
Null oblivious (i.e., no assumption in either direction)
What is null oblivious for F#?
No information about nullability for reference types in a given scope
If an oblivious value is given a nullable reference type, it is nullable
If an oblivious value is given a non-nullable reference type, it is non-nullable
Interesting question: What if ‘s’ is not annotated?
The case for null oblivious
Pros:
◦ Less warnings (if you don’t want them)
◦ Makes porting existing code a lot easier
◦ Logically consistent
Cons:
◦ Less warnings (if you do want them)
◦ More potential NREs
◦ Entire programs can be oblivious due to type inference
The case for assuming nullability
Pros:
◦ More warnings (if you want them)
◦ Less NREs if you account for the warnings
◦ Avoids completely oblivious programs
Cons:
◦ More warnings (if you don’t want them)
◦ Not logically consistent – no information means no assumptions, so why make them?
There is no perfect approach
Some users may prefer obliviousness
Some users may prefer assuming nullability
Getting it right will be a challenge
Current design is not set in stone (yet!)
Adjusting semantics for existing code
DEALING WITH PAST DECISIONS IS HARD
Default Values
Attribute implies nullability
'string’ is non-nullable
This valid pre-F# 5.0 code now produces a warning
CLIMutable
Attribute implies nullability
‘string’ is non-nullable
This valid pre-F# 5.0 code now produces a warning
Compiled form of F# options
• F# options compile to null for None case
• Changing this would be a massive source-
breaking change
• Ironically, F# options would be interpreted as
nullable reference types
• No good resolution to this quite yet
Why do this feature?
WAXING PHILOSOPHICAL ON NULLS
Make .NET a better place
Learn more
aka.ms/fsharpnulls
github.com/fsharp/fslang-designs
github.com/dotnet/csharplang

Contenu connexe

Tendances

Java Design Pattern Interview Questions
Java Design Pattern Interview QuestionsJava Design Pattern Interview Questions
Java Design Pattern Interview Questions
jbashask
 
Exception handling.41
Exception handling.41Exception handling.41
Exception handling.41
myrajendra
 

Tendances (16)

Java Design Pattern Interview Questions
Java Design Pattern Interview QuestionsJava Design Pattern Interview Questions
Java Design Pattern Interview Questions
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
 
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift ApplicationsHack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
 
The modern view on implementation of classic design patterns in Java
The modern view on implementation of classic design patterns in JavaThe modern view on implementation of classic design patterns in Java
The modern view on implementation of classic design patterns in Java
 
Introduction of C#
Introduction of C#Introduction of C#
Introduction of C#
 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
 
JetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem AspectJetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem Aspect
 
What To Expect With C
What To Expect With CWhat To Expect With C
What To Expect With C
 
Basic design pattern interview questions
Basic design pattern interview questionsBasic design pattern interview questions
Basic design pattern interview questions
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
Xtext: writing a grammar
Xtext: writing a grammarXtext: writing a grammar
Xtext: writing a grammar
 
Taxonomic key
Taxonomic keyTaxonomic key
Taxonomic key
 
Taxonomic keys
Taxonomic keysTaxonomic keys
Taxonomic keys
 
JetBrains MPS: Structure Aspect
JetBrains MPS: Structure AspectJetBrains MPS: Structure Aspect
JetBrains MPS: Structure Aspect
 
Exception handling.41
Exception handling.41Exception handling.41
Exception handling.41
 
Projectional editing
Projectional editingProjectional editing
Projectional editing
 

Similaire à Designing Nullable Reference Types in F#

AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
Miklos Csere
 

Similaire à Designing Nullable Reference Types in F# (20)

Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 
A Pragmatic Approach
A Pragmatic ApproachA Pragmatic Approach
A Pragmatic Approach
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Python typing module
Python typing modulePython typing module
Python typing module
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design Principles
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
Crafty communications - Dealing with the pesky people parts of communications
Crafty communications - Dealing with the pesky people parts of communicationsCrafty communications - Dealing with the pesky people parts of communications
Crafty communications - Dealing with the pesky people parts of communications
 
Presentation
PresentationPresentation
Presentation
 
The Psychology of C# Analysis
The Psychology of C# AnalysisThe Psychology of C# Analysis
The Psychology of C# Analysis
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdf
 
F# for startups
F# for startupsF# for startups
F# for startups
 
The OO Design Principles
The OO Design PrinciplesThe OO Design Principles
The OO Design Principles
 
Simplicitly
SimplicitlySimplicitly
Simplicitly
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
PL Lecture 03 - Types
PL Lecture 03 - TypesPL Lecture 03 - Types
PL Lecture 03 - Types
 

Dernier

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Designing Nullable Reference Types in F#

  • 1. Designing nullable reference types in F# PHILLIP CARTER - @_CARTERMP NDC LONDON – JANUARY 2019
  • 2. Overview Contextualizing nullability Design overview Metadata representation Nullability assumptions Adjusting semantics of existing F# code Why do this feature?
  • 4. Null itself is perfectly valid It really is! You need to represent ”nothing” in a programming language null accomplishes the task of representing “nothing”
  • 5. Implicit null is the problem Why no warning?
  • 9. Problems: AllowNullLiteral attribute I just needed nullability here I got nullability everywhere instead
  • 10. Problems: CLIMutable Can be made null in an application’s lifetime!
  • 11. Problems: General .NET interop Null? Null?
  • 12. Design overview DUE TO TIME CONSTRAINTS, THIS IS NOT COMPREHENSIVE
  • 13. Main goals Distinguish between nullable and non-nullable reference types in the F# type system Make nullable reference types less desirable to use than non-nullable reference types Offer helper functionality and patterns for dealing with nulls Offer compiler analysis for unsafe access of nullable reference types
  • 14. Syntax and basic examples
  • 15. More syntax and basic examples
  • 17. Generic constraints ‘T when ‘T: null ‘T when ‘T: struct ‘T when ‘T: not struct ‘T when ‘T: not null New! Semantics: • Error if the constraint is instantiated with an F# type that has null as a proper value • Warning if constraint is instantiated with a nullable reference type
  • 18. Pattern matching (majority case) NonNull pattern can be used anywhere, not just at the end ‘null’ pattern MUST be the first for this ‘s’ to be non-null, as per existing rules
  • 19. Pattern matching (column-based) s1 and s2 are still nullable! Typechecking proceeds before column analysis May need to use NonNull patterns to handle this
  • 20. F# type relations with nullability Type equivalence (x = y; y <> z) ◦ Nullness ignored, but warning given on mismatch Type subsumption (B a subtype of A) ◦ Nullness ignored, but warning given on mismatch Method overload resolution ◦ Nullness ignored, but warning given on mismatches in arguments and return types Abstract slot inference ◦ Nullness ignored, but warning given on slot mismatches Duplicate method checking ◦ Nullness is completely ignored
  • 21. Type inference and mixing null/non-null • First element in each collection is a non-null string • Introduction of nullable string introduces type equivalence mismatch • Solution is to add explicit type annotation
  • 22. Type inference and nullability assertions We cannot associate any arbitrary bool value with nullability. A nullability assertion is needed.
  • 23. Nullability and obj Nullability is completely ignored for ‘obj’ type The ‘obj’ type is used when type information is thrown out Making this nullable/non-nullable is pointless considering throwing out type information
  • 24. Null analysis for nullable value types Partial alignment of existing nullable value types is possible ◦ Same syntax may be possible (int?, float?, etc.) ◦ Same or similar rules for type relations and type inference
  • 25. Metadata HOW ALL THE INFORMATION GETS ENCODED
  • 27. Attributes to affect compiler analysis [<NotNullWhenTrue>] and [<NotNullWhenFalse>] ◦ Used in bool-returning functions or methods that check for null [<EnsuresNotNull>] ◦ Used on functions or methods that throw an exception when null is encountered [<AssertsTrue>] and [<AssertsFalse>] ◦ Used for asserting nullability, such as in test frameworks, and avoiding warnings
  • 28. [<NonNullTypes>] attribute Three cases: • NonNullTypes(true) • Nullability is a concept • NonNullTypes(false) • Nullability is not a concept • No attribute at all • Null oblivious semantics
  • 30. Three choices involved Distinguish between nullable and non-nullable reference types Do not distinguish between nullable and non-nullable reference types Null oblivious (i.e., no assumption in either direction)
  • 31. What is null oblivious for F#? No information about nullability for reference types in a given scope If an oblivious value is given a nullable reference type, it is nullable If an oblivious value is given a non-nullable reference type, it is non-nullable Interesting question: What if ‘s’ is not annotated?
  • 32. The case for null oblivious Pros: ◦ Less warnings (if you don’t want them) ◦ Makes porting existing code a lot easier ◦ Logically consistent Cons: ◦ Less warnings (if you do want them) ◦ More potential NREs ◦ Entire programs can be oblivious due to type inference
  • 33. The case for assuming nullability Pros: ◦ More warnings (if you want them) ◦ Less NREs if you account for the warnings ◦ Avoids completely oblivious programs Cons: ◦ More warnings (if you don’t want them) ◦ Not logically consistent – no information means no assumptions, so why make them?
  • 34. There is no perfect approach Some users may prefer obliviousness Some users may prefer assuming nullability Getting it right will be a challenge Current design is not set in stone (yet!)
  • 35. Adjusting semantics for existing code DEALING WITH PAST DECISIONS IS HARD
  • 36. Default Values Attribute implies nullability 'string’ is non-nullable This valid pre-F# 5.0 code now produces a warning
  • 37. CLIMutable Attribute implies nullability ‘string’ is non-nullable This valid pre-F# 5.0 code now produces a warning
  • 38. Compiled form of F# options • F# options compile to null for None case • Changing this would be a massive source- breaking change • Ironically, F# options would be interpreted as nullable reference types • No good resolution to this quite yet
  • 39. Why do this feature? WAXING PHILOSOPHICAL ON NULLS
  • 40. Make .NET a better place