0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Type Safety

Last updated at Posted at 2024-10-27

Cadence プログラミング言語は型安全言語(type-safe language)です。

変数に新しい値を割り当てる場合、その値は変数と同じ型でなければなりません。例えば、変数がBool型の場合、割り当てられる値はBool型のみであり、例えばInt型は不可です。

// Declare a variable that has type `Bool`.
var a = true

// Invalid: cannot assign a value that has type `Int` to a variable which has type `Bool`.
//
a = 0

関数に引数を渡す場合、値の型は関数のパラメータの型と一致していなければなりません。例えば、関数がBool型の引数を期待している場合、Bool型の値のみ渡すことができ、例えばInt型の値は渡せません。

fun nand(_ a: Bool, _ b: Bool): Bool {
    return !(a && b)
}

nand(false, false)  // is `true`

// Invalid: The arguments of the function calls are integers and have type `Int`,
// but the function expects parameters booleans (type `Bool`).
//
nand(0, 0)

型は自動的に変換されません。例えば、整数型は自動的にブーリアン型に変換されません。また、Int32は自動的にInt8に変換されません。また、オプショナルの整数型Int?は自動的にオプショナルではない整数型Intに変換されません。また、その逆も同じです。

fun add(_ a: Int8, _ b: Int8): Int8 {
    return a + b
}

// The arguments are not declared with a specific type, but they are inferred
// to be `Int8` since the parameter types of the function `add` are `Int8`.
add(1, 2)  // is `3`

// Declare two constants which have type `Int32`.
//
let a: Int32 = 3_000_000_000
let b: Int32 = 3_000_000_000

// Invalid: cannot pass arguments which have type `Int32` to parameters which have type `Int8`.
//
add(a, b)

翻訳元->https://cadence-lang.org/docs/language/type-safety

Flow BlockchainのCadence version1.0ドキュメント (Type Safety)

Previous << Scope

Next >> Type Inference(型推論)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?