Infer types from variable names

24 April 2024

Many programming languages with static typing infer types from expressions when possible. However, it is interesting that most (if not all of them) programming languages are missing out on opportunity to infer types from variable names.

There are lots of certain naming patterns formed in most of the languages. For example, it is a common practice to start a boolean variable name with is in JS or end it with ? in Ruby. But why not make it a part of the language type system?

Definition of the variable could be a bit simpler (in pseudocode):

def bool is_valid = false

could be:

def is_valid = false

Interpreter or compiler could rely on variable names in addition to inference from usage. Interpreter could also add this bool internally or use other tricks. We, humans should not care much about this.

Benefits are obvious:

  1. Variables naming gets easier.
  2. Stricter conventions enforce consistency across codebase.
  3. Less code.

What is it that prevents us from doing this?