Skip to content

NEP17 Type Inference

Greg Hewgill edited this page Sep 28, 2020 · 1 revision

This proposal suggests removing required type specifications for variable declarations.

Motivation

Currently, types are required for all variable declarations. This causes code to become more cluttered than it probably needs to be:

LET length: Number := 5
LET name: String := "widget"
LET description: String := "\(name), \(length)"

All the type specifications (: Number and : String) are technically redundant because the type is obvious from the right hand side.

Proposal

This propsal makes type specifications optional for CONSTANT, VAR, and LET statements. The above code sample would look like:

LET length := 5
LET name := "widget"
LET description := "\(name), \(length)"

Type specifications may still be provided if desired for clarity.

Type specifications still required in some cases

There are a few cases where type specifications are still required:

  • Empty arrays or dictionaries: VAR a := [] is not valid because there is no way to determine what the type of the array elements should be.
  • Pointers initialized to NIL: The statement VAR p := NIL requires a type specification to specify what class p points to.
  • Conversions to and from Object: LET obj := 5 infers that obj should be a Number. To convert to an Object, use LET obj: Object := 5.