Jump to content

Type inference

From Simple English Wikipedia, the free encyclopedia

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.

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

[change | change source]
  1. "WG14-N3007 : Type inference for object definitions". open-std.org. 2022-06-10. Archived from the original on December 24, 2022.
  2. "Placeholder type specifiers (since C++11) - cppreference.com". en.cppreference.com. Retrieved 2021-08-15.
  3. "The Dart type system". dart.dev. Retrieved 2020-11-21.
  4. cartermp. "Type Inference - F#". docs.microsoft.com. Retrieved 2020-11-21.
  5. "Inference · The Julia Language". docs.julialang.org. Retrieved 2020-11-21.
  6. "Kotlin language specification". kotlinlang.org. Retrieved 2021-06-28.
  7. "Statements - The Rust Reference". doc.rust-lang.org. Retrieved 2021-06-28.
  8. "Type Inference". Scala Documentation. Retrieved 2020-11-21.
  9. "The Basics — The Swift Programming Language (Swift 5.5)". docs.swift.org. Retrieved 2021-06-28.
  10. "Documentation - Type Inference". www.typescriptlang.org. Retrieved 2020-11-21.
  11. Trex, Nova (24 December 2024). V Programming: Building Robust and Efficient Software Systems. p. 14. ASIN B0DRJMP1HM. ISBN 979-8304813778.