Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active February 5, 2016 17:17
Show Gist options
  • Save alphaKAI/5fe2e6a4122182d02051 to your computer and use it in GitHub Desktop.
Save alphaKAI/5fe2e6a4122182d02051 to your computer and use it in GitHub Desktop.
Immutable と constの簡単な違い
import std.stdio;
void immutableTest() {
immutable int N = 10;//変更不可能
immutable int* ptr = &N;//変更不可能なポインタ
//ptr = &N;当然不可能
// このコードは動かない
// immutableなポインタはimmutableな変数しかとれない
/*
int M = 10;
immutable int* ptr2 = &M;
writeln(M);
assert(M == 10);
M = 20;
*/
}
void constTest() {
const int N = 10;
const int* ptr = &N;
//ptr = &N;当然不可能
int M = 10;
const int* ptr2 = &M;
writeln(M);
assert(M == 10);
M = 20;
assert(M == 20);
//*ptr = 100;これはできないけど
M = 100;
assert(*ptr2 == 100);
}
void main() {
immutableTest;
constTest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment