Hi, I'm Ataias Reis

Programmer, made in Brazil, Alma mater University of Brasilia

What I think is missing in C: Type Inference

The first programming language I learned was C and I found it pretty interesting back then. Nevertheless, I have seen a couple of languages in the last years and I really think C is missing something important: type inference. The concept is simple and it means that the compiler will try to infer what the variable type is for you. You need to give context in the code for this to happen, but I have seen it work well on OCaml and Golang. As a simple example in Go:

package main

import "fmt"

func main() {
	v := 42.5
	m := 40
	fmt.Printf("v is of type %T\n", v)
	fmt.Printf("m is of type %T\n", m)

The output of the program is

v is of type float64
m is of type int

Type Inference gives more work for the compiler, but at least code will be prettier to look at and I think it is easier to maintain too. Notice that this is not dynamic typing but static typing! That’s the main beauty of it: the compiler arranges everything so that it compiles statically what you need and this is usually faster than dynamic typing as far as I know.

That’s all for this post. Thanks for reading!

comments powered by Disqus