Language: EN


Type Deduction by Context

The type deduction of a variable by context is a modern feature of several programming languages that allows compilers or interpreters to automatically infer the type of a variable based on the context in which it is declared.

The choice between typed and untyped languages has been a constant topic of debate in the field of programming.

One undeniable advantage of dynamically typed languages such as JavaScript or Python is that the syntax is more concise and simple, avoiding the need to specify the type of the variables we create.

However, as programming languages evolve, mechanisms have also emerged to simplify the verbosity of typed languages, without sacrificing the safety and robustness provided by typing.

Thus, languages like C# and C++ use the reserved word var or auto, respectively, to indicate to the compiler that we want it to deduce the type of the variable during compilation.

Examples of type deduction in different languages

Let’s look at some examples of type deduction by context in different programming languages.

In C#, the var keyword is used to deduce the type of a variable based on the initialization expression.

var number = 42; // The compiler deduces that 'number' is of type int
var text = "Hello, world"; // The compiler deduces that 'text' is of type string
var list = new List<string> { "one", "two", "three" }; // The compiler deduces that 'list' is of type List<string>

In C++, the auto keyword is used to indicate that the type of the variable should be deduced from the initialization value. This is especially useful for long or complicated types.

auto number = 42; // The compiler deduces that 'number' is of type int
auto text = std::string("Hello, world"); // The compiler deduces that 'text' is of type std::string
auto list = std::vector<std::string>{"one", "two", "three"}; // The compiler deduces that 'list' is of type std::vector<std::string>

In JavaScript, type deduction is implicit and does not require special keywords. The type of a variable is automatically inferred based on the assigned value.

let number = 42; // The interpreter deduces that 'number' is of type Number
let text = "Hello, world"; // The interpreter deduces that 'text' is of type String
let list = ["one", "two", "three"]; // The interpreter deduces that 'list' is of type Array

In Python, variable types are automatically inferred at the time of assignment. It is not necessary to specify the type, as Python is a dynamically typed language.

number = 42 # Python deduces that 'number' is of type int
text = "Hello, world" # Python deduces that 'text' is of type str
list = ["one", "two", "three"] # Python deduces that 'list' is of type list

Best practices Tips

Type deduction by context reduces repetitive code, and avoids the redundancy of having to explicitly specify the type when it is already evident from the assignment.

Additionally, it improves code readability. For example, consider the following case in C#.

// it is easier to read
var list = new List<string> { "one", "two", "three" }; 

// than this
List<string> list = new List<string> { "one", "two", "three" }; 

On the other hand, it also facilitates maintenance. By not depending on an explicit declaration, changes in data types are automatically propagated if the initial assignment changes.

For example, if we have a function that returns a type,

// method that returns an object of type myClass
myClass DoSomething()
	// ... function content	

// many lines down, create an object with the method
var myObject = DoSomething();

If at some point someone changes DoSomething to return something else (for example, ImyInterface), it is not necessary to change var myObject = DoSomething();, the changes are automatically “propagated”.

However, there are also many detractors of type deduction by context. Especially people who are very accustomed to programming with strongly typed languages.

Thus, it is usually advised not to use it in situations where the type is not obvious. For example,

// here the type is obvious
var mylist = new List<string>();

// here the type is not obvious
List<string> mylist = myMethod();

In the end, the decision is yours. Personally, I recommend using type deduction by context everywhere. But it’s not an absolute truth. It’s best to make your own judgment.