www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile error using synchronized in ldc (but not dmd)

reply "Nikhil Padmanabhan" <nikhil.padmanabhan gmail.com> writes:
Hello,

The following code snippet fails to compile on ldc2 (0.12.1), but 
successfully compiles on dmd 2.064.2 :

struct Particle {
	double x,y,z,w,x2;
	this(double[] arr) {
		x = arr[0]; y = arr[1]; z=arr[2]; w=arr[3];
		x2 = x*x + y*y + z*z;
	}
}

synchronized class SyncArray {
	private Particle[] buf;

	void push(Particle[] arr1) {
		buf.length = arr1.length;
		buf[] = arr1[];
	}

	Particle[] pop() {
		auto _tmp = new Particle[buf.length];
		_tmp[] = buf[];
		buf = null;
		return _tmp;
	}
}

The error message in ldc2 :
test.d(14): Error: cannot implicitly convert expression (arr1[]) 
of type Particle[] to const(shared(Particle)[])
test.d(19): Error: cannot implicitly convert expression 
(this.buf[]) of type shared(Particle)[] to const(Particle[])

What am I doing wrong here?

Thanks in advance!
-- Nikhil

P.S. I've been enjoying coding in D a lot -- many thanks to the 
entire D community!
Dec 08 2013
parent "Nikhil Padmanabhan" <nikhil.padmanabhan gmail.com> writes:
Just a quick follow-up : replacing the array operation by a 
foreach works around this issue, but I don't understand why it 
failed in the first place.


On Monday, 9 December 2013 at 04:26:01 UTC, Nikhil Padmanabhan 
wrote:
 Hello,

 The following code snippet fails to compile on ldc2 (0.12.1), 
 but successfully compiles on dmd 2.064.2 :

 struct Particle {
 	double x,y,z,w,x2;
 	this(double[] arr) {
 		x = arr[0]; y = arr[1]; z=arr[2]; w=arr[3];
 		x2 = x*x + y*y + z*z;
 	}
 }

 synchronized class SyncArray {
 	private Particle[] buf;

 	void push(Particle[] arr1) {
 		buf.length = arr1.length;
 		buf[] = arr1[];
 	}

 	Particle[] pop() {
 		auto _tmp = new Particle[buf.length];
 		_tmp[] = buf[];
 		buf = null;
 		return _tmp;
 	}
 }

 The error message in ldc2 :
 test.d(14): Error: cannot implicitly convert expression 
 (arr1[]) of type Particle[] to const(shared(Particle)[])
 test.d(19): Error: cannot implicitly convert expression 
 (this.buf[]) of type shared(Particle)[] to const(Particle[])

 What am I doing wrong here?

 Thanks in advance!
 -- Nikhil

 P.S. I've been enjoying coding in D a lot -- many thanks to the 
 entire D community!
Dec 08 2013