www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shared immutable issues

reply "Enerqi" <kelvin.d.ward googlemail.com> writes:
Hi

I'm wondering what the state of the language/libraries are with 
regard to shared/immutable qualifiers. I've tried to use 
std.concurrency and had a lot of compile issues passing shared 
data around.
Even a simple case I'm finding issue with:

---------
import std.array;

int main(string[] argv)
{
	shared uint[] arr = stuffone();	
	return 0;
}

shared uint[] stuffone()
{
	shared uint[] arr = new shared (uint[1]);
	arr[0] = 1;	
	return cast(shared)arr;
}

---------
Compile error:
shared.d(6): Error: cannot implicitly convert expression 
(stuffone()) of type uint[] to shared(uint[])
shared.d(15): Error: cannot implicitly convert expression (arr) 
of type shared(uint[]) to uint[]

Another case I couldn't construct an appender to shared data:
---------
import std.array;

int main(string[] argv)
{
	Appender!(shared uint[]) app = std.array.appender!(shared 
uint[]);
	return 0;
}
---------
Compile error:

Error: incompatible types for ((cast(shared(uint)*)arr) is 
(cast(uint*)(*this._data).arr)): 'shared(uint)*' and 'uint*'
shared.d(6): Error: template instance 
std.array.Appender!(shared(uint[])) error instantiating
shared.d(6): Error: constructor 
std.array.Appender!(shared(uint[])).Appender.this (shared(uint)[] 
arr) is not callable using argument types 
(Appender!(shared(uint)[]))
shared.d(6): Error: cannot implicitly convert expression 
(appender(null)) of type Appender!(shared(uint)[]) to 
shared(uint)[]
std\array.d(1878): Error: incompatible types for 
((cast(shared(uint)*)arr) is (cast(uint*)(*this._data).arr)): 
'shared(uint)*' and 'uint*'


Not sure if I'm missing something obvious.
Thanks.
Jul 23 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/23/2012 10:21 AM, Enerqi wrote:
 Hi

 I'm wondering what the state of the language/libraries are with regard
 to shared/immutable qualifiers. I've tried to use std.concurrency and
 had a lot of compile issues passing shared data around.
 Even a simple case I'm finding issue with:

 ---------
 import std.array;

 int main(string[] argv)
 {
 shared uint[] arr = stuffone();
 return 0;
 }

 shared uint[] stuffone()

Hmmm... I think that's a bug. It should be the equivalent of the following, which does work: shared(uint[]) stuffone() { shared uint[] arr = new shared (uint[1]); arr[0] = 1; return arr; } Note that no need to cast returned object any more.
 Another case I couldn't construct an appender to shared data:
 ---------
 import std.array;

 int main(string[] argv)
 {
 Appender!(shared uint[]) app = std.array.appender!(shared uint[]);
 return 0;
 }
 ---------
 Compile error:

 Error: incompatible types for ((cast(shared(uint)*)arr) is
 (cast(uint*)(*this._data).arr)): 'shared(uint)*' and 'uint*'
 shared.d(6):

Appender does cast(Unqual!(T)) which removes the 'shared' qualifier. I think that should be considered a bug with Appender. Ali
Jul 23 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, July 23, 2012 10:37:03 Ali Çehreli wrote:
 Appender does cast(Unqual!(T)) which removes the 'shared' qualifier. I
 think that should be considered a bug with Appender.

Unqual is used all over the place in Phobos to remove const. I don't think that shared is considered much at all at this point. And given the various issues with shared, you generally have to temporarily cast it to non-shared to do anything with it anyway. shared is one of those things that may be okay in principle but which hasn't had all of its minor details properly sorted out yet. - Jonathan M Davis
Jul 23 2012
prev sibling next sibling parent "Enerqi" <kelvin.d.ward googlemail.com> writes:
Thanks for the feedback guys. Casting away shared before usage 
works.

On Monday, 23 July 2012 at 21:02:23 UTC, Jonathan M Davis wrote:
 On Monday, July 23, 2012 10:37:03 Ali Çehreli wrote:
 Appender does cast(Unqual!(T)) which removes the 'shared' 
 qualifier. I
 think that should be considered a bug with Appender.

Unqual is used all over the place in Phobos to remove const. I don't think that shared is considered much at all at this point. And given the various issues with shared, you generally have to temporarily cast it to non-shared to do anything with it anyway. shared is one of those things that may be okay in principle but which hasn't had all of its minor details properly sorted out yet. - Jonathan M Davis

Jul 24 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, July 24, 2012 11:08:06 Enerqi wrote:
 Thanks for the feedback guys. Casting away shared before usage
 works.

The situation really needs to be improved, but that's pretty much what you're forced to do right now. - Jonathan m Davis
Jul 24 2012