dlang-book/04-массивы-ассоциативные-ма.../src/chapter-4-2-4/app.d

34 lines
961 B
D
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import std.stdio;
int[3] fun(int[3] x, int[3] y)
{
// x и y ко­пии пе­ре­дан­ных ар­гу­мен­тов
x[0] = y[0] = 100;
return x;
}
double[3] fun2(double[] x)
{
double[3] result;
result[] = 2 * x[]; // Опе­ра­ция над мас­си­вом в це­лом
return result;
}
void main()
{
int[3] a = [1, 2, 3];
int[3] b = a;
a[1] = 42;
assert(b[1] == 2); // b не­за­ви­си­мая ко­пия a
auto c = fun(a, b); // c име­ет тип int[3]
assert(c == [100, 42, 3]);
writeln(c);
assert(b == [1, 2, 3]); // Вы­зов fun ни­как на от­ра­зил­ся на b
writeln(b);
double[3] point = [0, 0, 0];
double[] test = point; // Все в по­ряд­ке
auto r = fun2(point); // Все в по­ряд­ке, те­перь r име­ет тип double[3]
}