Flow-sensitive typing: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Alter: title. | You can use this bot yourself. Report bugs here. | Suggested by Headbomb | Category:CS1 errors: invisible characters | via #UCB_Category 86/428
Add "pattern matching" as an alternative to flow-sensitive typing
Line 81:
 
There are also a few languages that don't have [[Tagged union|union types]] but do have [[nullable types]], that have a limited form of this feature that only applies to nullable types, such as [[C Sharp (programming language)|C#]],<ref>{{cite web |title=Design with nullable reference types |url=https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types#create-respondents-and-get-answers-to-the-survey |website=docs.microsoft.com |language=en-us}}</ref> [[Kotlin (programming language)|Kotlin]],<ref>{{cite web | url=https://kotlinlang.org/docs/reference/null-safety.html | title=Null Safety | publisher=kotlinlang.org | accessdate=11 March 2016}}</ref><ref>{{cite web | url=https://kotlinlang.org/docs/reference/typecasts.html | title=Type Checks and Casts | publisher=kotlinlang.org | accessdate=11 March 2016}}</ref> and Lobster.<ref>{{cite web |title=The Lobster Type System |url=http://aardappel.github.io/lobster/type_checker.html#the-trouble-with-nil |website=aardappel.github.io}}</ref>
 
==Alternatives==
 
[[Pattern matching]] reaches the same goals as flow-sensitive typing, namely reducing [[verbosity]] and making up for terser code, easier to read and modify.
It achieves this is in a different way, it allows to match the type of a structure, extract data out of it at the same time by declaring new variable. As such, it reduces the ceremony around type casting and value extraction. Pattern matching works best when used in conjunction with [[algebraic data types]] because all the cases can be enumerated and statically checked by the compiler.
 
In a statically typed language, the advantage of pattern matching over flow-sensitive typing is that the type of a variable always stays the same: it does not change depending on control flow. When writing down the pattern to be matched, a new variable is declared that will have the new type.
 
==External references==