ref vs out.
Charles McAnany
mcanance at rose-hulman.edu
Mon Feb 14 07:06:51 PST 2011
Hi, all. So I'm new to this whole contract thing. (I'm coming from C and Java.)
I got the impression that using foo(out arg) means that arg is given its
default value, but other than that it's just like ref. So, here's the basic
code I have thus far.
01 import std.random:Random;
02 import std.container:heapify;
03 Random gen;
04 void main(){
05 auto start = new int[n];
06 randomize(start);
07 auto theHeap = heapify(start);
08 int temp = theHeap.front;
09 }
10 void randomize(ref int[] arr){
11 foreach(ref i; arr){
12 i = gen.front % 10000;
13 gen.popFront;
14 }
15 }
This compiles and runs.
However, if I switch the method signature to randomize(out int[] arr)
I get
object.Exception at C:\D\dmd2\windows\bin\..\..\src\phobos\std\container.d(2533):
Enforcement failed
(This is a run-time error, it compiles fine.)
What's really perplexing is that the problem comes from line 8.
If I comment out 8, then there's no enforcement error.
If it helps, container.d includes
2531 @property ElementType!Store front()
2532 {
2533 enforce(!empty);
2534 return _store.front;
2545 }
Thanks,
Charles
More information about the Digitalmars-d-learn
mailing list