-
Notifications
You must be signed in to change notification settings - Fork 3
NEP17 Type Inference
Greg Hewgill edited this page Sep 28, 2020
·
1 revision
This proposal suggests removing required type specifications for variable declarations.
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.
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.
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 statementVAR p := NIL
requires a type specification to specify what classp
points to. - Conversions to and from
Object
:LET obj := 5
infers thatobj
should be aNumber
. To convert to anObject
, useLET obj: Object := 5
.