1
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 Annotations

Last updated at Posted at 2024-10-29

Previous << Constants and Variable Declarations
Next >> Values and Types

定数または変数を宣言する際には、宣言の型を明示するために、オプションで型アノテーションを指定することができます。

型アノテーションが指定されていない場合、宣言の型は初期値から推論されます

関数のパラメータには、型アノテーションを指定する必要があります。

/* Declare a variable named `boolVarWithAnnotation`, which has an explicit type annotation.
 *
 * `Bool` is the type of booleans.
 */
var boolVarWithAnnotation: Bool = false

/* Declare a constant named `integerWithoutAnnotation`, which has no type annotation
 * and for which the type is inferred to be `Int`, the type of arbitrary-precision integers.
 *
 * This is based on the initial value which is an integer literal.
 * Integer literals are always inferred to be of type `Int`.
 */
let integerWithoutAnnotation = 1

/* Declare a constant named `smallIntegerWithAnnotation`, which has an explicit type annotation.
 * Because of the explicit type annotation, the type is not inferred.
 * This declaration is valid because the integer literal `1` fits into the range of the type `Int8`,
 * the type of 8-bit signed integers.
 */
let smallIntegerWithAnnotation: Int8 = 1

型アノテーションが提供されている場合、初期値は必ずその型でなければなりません。変数に割り当てられるすべての新しい値はその型と一致しなければなりません。この型の安全性については、separateセクションでさらに詳しく説明します。

/* Invalid: declare a variable with an explicit type `Bool`,
 * but the initial value has type `Int`.
 */
let booleanConstant: Bool = 1

/* Declare a variable that has the inferred type `Bool`. */
var booleanVariable = false

/* Invalid: assign a value with type `Int` to a variable which has the inferred type `Bool`. */
booleanVariable = 1

翻訳元


Previous << Constants and Variable Declarations

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

Next >> Values and Types

1
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
1
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?