Type inference
automatic detection of the data type of an expression in a programming language From Wikipedia, the free encyclopedia
Remove ads
Type inference (also inferred and infer), is when the type of a variable or missing word is decided in an automatic way using rules to check if it is correct for a specific purpose. For example, we could fill in the blank (_) of a phrase "sing a _." If "song" is used to replace the blank, then we get the usable phrase of "sing a song." If instead we replaced the blank with the word "friend", in English, we get a type of phrase that makes no sense. The phrase is correct based on the word chosen and English rules of grammar.
In the below example, we will apply this logic to a variable (var
) of the integer type (int
):
var = 1 // the type of variable is unknown
int var = 1 // the type of variable is known (integer)
In various programming languages, the type can be inferred by the compiler and its internal rules. The programmer does not have to decide and instead the compiler does so.
Remove ads
Uses
Some notable languages that include type inference: C,[1] C++,[2] C# (from version 3.0), D, [3] F#,[4] FreeBASIC, Go, Haskell, Java (from version 10), Julia,[5] Kotlin,[6] OCaml, Rust,[7] Scala,[8] Swift,[9] TypeScript,[10] and V (Vlang).[11]
References
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads