The Cadence programming language is a type-safe language.
When assigning a new value to a variable, the value must be the same type as the variable.
For example, if a variable has type Bool
,
it can only be assigned a value that has type Bool
,
and not for example a value that has type Int
.
1// Declare a variable that has type `Bool`.2var a = true34// Invalid: cannot assign a value that has type `Int` to a variable which has type `Bool`.5//6a = 0
When passing arguments to a function,
the types of the values must match the function parameters' types.
For example, if a function expects an argument that has type Bool
,
only a value that has type Bool
can be provided,
and not for example a value which has type Int
.
1fun nand(_ a: Bool, _ b: Bool): Bool {2return !(a && b)3}45nand(false, false) // is `true`67// Invalid: The arguments of the function calls are integers and have type `Int`,8// but the function expects parameters booleans (type `Bool`).9//10nand(0, 0)
Types are not automatically converted.
For example, an integer is not automatically converted to a boolean,
nor is an Int32
automatically converted to an Int8
,
nor is an optional integer Int?
automatically converted to a non-optional integer Int
,
or vice-versa.
1fun add(_ a: Int8, _ b: Int8): Int8 {2return a + b3}45// The arguments are not declared with a specific type, but they are inferred6// to be `Int8` since the parameter types of the function `add` are `Int8`.7add(1, 2) // is `3`89// Declare two constants which have type `Int32`.10//11let a: Int32 = 3_000_000_00012let b: Int32 = 3_000_000_0001314// Invalid: cannot pass arguments which have type `Int32` to parameters which have type `Int8`.15//16add(a, b)