2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JuliaのRef

Posted at

Juliaの分散関係のコードを読んでいたら、こんなコードを見つけて。

const REF_ID = Ref(1)
next_ref_id() = (id = REF_ID[]; REF_ID[] = id+1; id)

どうも、インクリメントしてユニークなIDを作っているらしい。Refというのは参照をとりだすもので、[]という演算子はderefするものらしい。つまり、Cで書くと

static int counter = 1;
int * REF_ID = &counter;
int next_ref_id() {
   int id = *REF_ID;
   *REF_ID = id + 1;
   return id;
}

みたいなことをやっているらしい。

うーん、こんなことできるのか。おそるべしJulia。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?