Also ich habe noch keinen wirklichen Unterschied feststellen können, denn auch wenn ich mittels typeid die Parameter mir ausgeben lasse, sehe ich bei beiden const(PARAM). Auch bei diesem Test (wir brauchen hier mal einen D Syntax):
|
C# Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import std.stdio;
struct Node {
void print(in byte nr) const {
writefln("Node print [%d]", nr);
}
}
class Parent {
this(in Node n) {
n.print(0);
writeln(typeid(n));
}
this(const Node n) {
n.print(1);
writeln(typeid(n));
}
void init(in int[] array) {
writeln("in");
writeln(typeid(array));
//array[0] = array[0] + 42;
foreach (ref int e; array) {
writeln(e);
//e++;
}
}
void init_(const int[] array) {
writeln("const");
writeln(typeid(array));
//array[0] = array[0] + 42;
foreach (ref int e; array) {
writeln(e);
//e++;
}
}
}
void main() {
Node n = Node();
auto p = new Parent(n);
p.init([1, 2, 3, 4, 5, 6, 7, 8, 9,]);
p.init_([1, 2, 3, 4, 5, 6, 7, 8, 9,]);
}
|
(die auskommentierten Zeilen wurden mir angekriedet, gerade wegen der const Beziehung)
bekam ich zu aller erst dies hier:
C:\Users\White\Desktop\d zauberei>dmd const_ref.d
const_ref.d(42): Error: constructor const_ref.Parent.this called with argument t
ypes:
((Node))
matches both:
const_ref.Parent.this(in const(Node) n)
and:
const_ref.Parent.this(const const(Node) n)
Und nachdem ich den Konsturktor mit dem const Parameter auskommentiert hatte, dies:
Node print [0]
const(const_ref.Node)
in
const(const(int)[])
1
2
3
4
5
6
7
8
9
const
const(const(int)[])
1
2
3
4
5
6
7
8
9
Also scheint intern beides gleich behandelt zu werden.
Du würdest also const verwenden? Und wenn, weswegen? Aussagekräftiger, Gewohnheit?