www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shared Variant[string]

reply "Fyodor Ustinov" <ufm ufm.su> writes:
Hi!

Simple program:
import std.variant;

shared Variant[string] t;

void main() {
     t["t"] = "bebebe";
}

Without "shared" this program compiles and works.

With shared I get:

aa.d(6): Error: template 
std.variant.VariantN!32LU.VariantN.opAssign cannot deduce 
function from argument types !()(string) shared, candidates are:
/usr/include/dmd/phobos/std/variant.d(577):        
std.variant.VariantN!32LU.VariantN.opAssign(T)(T rhs)
aa.d(6): Error: cannot implicitly convert expression (VariantN(& 
handler, cast(ubyte)0u, ).this("bebebe")) of type VariantN!32LU 
to shared(VariantN!32LU)

Help me, please.
Jan 28 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
On Wednesday, 28 January 2015 at 10:16:02 UTC, Fyodor Ustinov 
wrote:
 Hi!

 Simple program:
 import std.variant;

 shared Variant[string] t;

 void main() {
     t["t"] = "bebebe";
 }

 Without "shared" this program compiles and works.

 With shared I get:

 aa.d(6): Error: template 
 std.variant.VariantN!32LU.VariantN.opAssign cannot deduce 
 function from argument types !()(string) shared, candidates are:
 /usr/include/dmd/phobos/std/variant.d(577):        
 std.variant.VariantN!32LU.VariantN.opAssign(T)(T rhs)
 aa.d(6): Error: cannot implicitly convert expression 
 (VariantN(& handler, cast(ubyte)0u, ).this("bebebe")) of type 
 VariantN!32LU to shared(VariantN!32LU)

 Help me, please.
try __gshared
Jan 28 2015
parent reply "Fyodor Ustinov" <ufm ufm.su> writes:
On Wednesday, 28 January 2015 at 10:20:42 UTC, zhmt wrote:

 try __gshared
It seems to me - is not the solution, it's a hack. I want to understand what the happening. :)
Jan 28 2015
next sibling parent "zhmt" <zhmtzhmt qq.com> writes:
On Wednesday, 28 January 2015 at 10:32:56 UTC, Fyodor Ustinov 
wrote:
 On Wednesday, 28 January 2015 at 10:20:42 UTC, zhmt wrote:

 try __gshared
It seems to me - is not the solution, it's a hack. I want to understand what the happening. :)
I am new to D. But I that error yesterday, and it's solved in that way, I dont understand the reason clearly.
Jan 28 2015
prev sibling next sibling parent reply "Kagamin" <spam here.lot> writes:
Associative array doesn't support thread-safe operations, that's 
why they don't work on shared instance. You should use 
std.concurrency or implement low-level concurrency mechanism.
Jan 28 2015
parent reply "Fyodor Ustinov" <ufm ufm.su> writes:
On Wednesday, 28 January 2015 at 11:27:53 UTC, Kagamin wrote:
 Associative array doesn't support thread-safe operations, 
 that's why they don't work on shared instance. You should use 
 std.concurrency or implement low-level concurrency mechanism.
If associative array does not support "share" attribute, this code should not be compiled without any warning or error, I think: shared string[string] t; void main() { t["t"] = "bebebe"; } But will.
Jan 28 2015
next sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Wednesday, 28 January 2015 at 12:29:09 UTC, Fyodor Ustinov 
wrote:
 On Wednesday, 28 January 2015 at 11:27:53 UTC, Kagamin wrote:
 Associative array doesn't support thread-safe operations, 
 that's why they don't work on shared instance. You should use 
 std.concurrency or implement low-level concurrency mechanism.
If associative array does not support "share" attribute, this code should not be compiled without any warning or error, I think: shared string[string] t; void main() { t["t"] = "bebebe"; } But will.
In your case above, it's std.variant : Variant, which does not work when shared. You'll need to cast it and ensure yourself that no two threads access the same instance concurrently. I don't know, if AA should/do work with shared.
Jan 28 2015
parent reply "Fyodor Ustinov" <ufm ufm.su> writes:
On Wednesday, 28 January 2015 at 12:32:29 UTC, Tobias Pankrath 
wrote:
 In your case above, it's std.variant : Variant, which does not 
 work when shared.
I look into variant.d and after seeing so many lines " BUG" - strange that it somehow works. :)
Jan 28 2015
parent "Meta" <jared771 gmail.com> writes:
On Wednesday, 28 January 2015 at 12:46:20 UTC, Fyodor Ustinov 
wrote:
 On Wednesday, 28 January 2015 at 12:32:29 UTC, Tobias Pankrath 
 wrote:
 In your case above, it's std.variant : Variant, which does not 
 work when shared.
I look into variant.d and after seeing so many lines " BUG" - strange that it somehow works. :)
AAs are a messy part of the language right now due to how they're implemented. A new implementation is being worked on but it's a tricky thing to fix.
Jan 28 2015
prev sibling parent "Dominikus Dittes Scherkl" writes:
On Wednesday, 28 January 2015 at 12:29:09 UTC, Fyodor Ustinov 
wrote:
 On Wednesday, 28 January 2015 at 11:27:53 UTC, Kagamin wrote:
 Associative array doesn't support thread-safe operations, 
 that's why they don't work on shared instance. You should use 
 std.concurrency or implement low-level concurrency mechanism.
If associative array does not support "share" attribute, this code should not be compiled without any warning or error, I think: shared string[string] t; void main() { t["t"] = "bebebe"; } But will.
string is a shorthand for "immutable char" - and immutables are shared by default. No thread can modify them, so default shared is safe for them.
Jan 29 2015
prev sibling parent "Kagamin" <spam here.lot> writes:
Some reading:
http://ddili.org/ders/d.en/concurrency.html
http://ddili.org/ders/d.en/concurrency_shared.html
http://www.informit.com/articles/article.aspx?p=1609144
Jan 28 2015