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?

Scope

Last updated at Posted at 2024-10-27

すべての関数およびブロック ({...}) は、宣言用の新しいスコープを導入します。各関数およびブロックは、そのスコープ内または外部のスコープ内の宣言を参照できます。

let x = 10

fun f(): Int {
    let y = 10
    return x + y
}

f()  // is `20`

// Invalid: the identifier `y` is not in scope.
//
y
fun doubleAndAddOne(_ n: Int): Int {
    fun double(_ x: Int) {
        return x * 2
    }
    return double(n) + 1
}

// Invalid: the identifier `double` is not in scope.
//
double(1)

各スコープは新しい宣言を導入することができ、すなわち、外側の宣言が覆い隠されます。

let x = 2

fun test(): Int {
    let x = 3
    return x
}

test()  // is `3`

スコープは字句的であり、動的ではありません。

let x = 10

fun f(): Int {
   return x
}

fun g(): Int {
   let x = 20
   return f()
}

g()  // is `10`, not `20`

宣言は、囲む関数の上に影響(hoist)しません。

let x = 2

fun f(): Int {
    if x == 0 {
        let x = 3
        return x
    }
    return x
}
f()  // is `2`

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

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

Previous << Control Flow

Next >> Type Safety

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?