www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why won't this compile using DMD?

reply "Ivan Trombley" <itrombley dot-borg.org> writes:
I'm stumped. Can anyone tell me why this won't compule using DMD 
(v2.060)? It compiles and runs just fine using GDC:

import std.stdio;
import std.concurrency;

shared class Data
{
public:
   this(int count)
   {
     m_count = count;
   }

   int count() const
   {
     return m_count;
   }

private:
   int m_count = void;
}

int main()
{
   Tid tid = spawn(&threadFunc, thisTid);
   while (true)
   {
     auto data = receiveOnly!(Data);
     writefln("Count is %s.", data.count);

     if (data.count == 0)
       break;
   }

   return 0;
}

void threadFunc(Tid owner)
{
   for (int count = 10; count >= 0; --count)
   {
     auto data = new Data(count);
     send(owner, data);
   }
}


The errors I get from DMD are:

test.d(27): Error: function test.Data.count () shared const is 
not callable using argument types ()
test.d(29): Error: function test.Data.count () shared const is 
not callable using argument types ()
/usr/include/dmd/phobos/std/concurrency.d(467): Error: static 
assert  "Aliases to mutable thread-local data not allowed."
test.d(41):        instantiated from here: send!(Data)
Aug 12 2012
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 12 Aug 2012 09:39:13 +0200, Ivan Trombley <itrombley dot-borg.org>  
wrote:

 I'm stumped. Can anyone tell me why this won't compule using DMD  
 (v2.060)? It compiles and runs just fine using GDC:

The problems all have to do with one thing - shared class Data does not mean every instance of the class is shared, only that all member functions are. Hence,
      auto data = receiveOnly!(Data);

should be auto data = receiveOnly!(shared Data); and
      auto data = new Data(count);

should be auto data = cast(shared)new Data(count); -- Simen
Aug 12 2012
parent "Ivan Trombley" <itrombley dot-borg.org> writes:
On Sunday, 12 August 2012 at 14:31:37 UTC, Simen Kjaeraas wrote:
 The problems all have to do with one thing - shared class Data 
 does not mean every instance of the class is shared, only that 
 all member functions are.

Excellent, thanks. I assumed that since the class was marked as shared then any instance of it would be as well.
Aug 12 2012
prev sibling next sibling parent =?ISO-8859-1?Q?Jos=E9_Armando_Garc=EDa_Sancio?= <jsancio gmail.com> writes:
On Sun, Aug 12, 2012 at 7:31 AM, Simen Kjaeraas <simen.kjaras gmail.com> wrote:
 On Sun, 12 Aug 2012 09:39:13 +0200, Ivan Trombley <itrombley dot-borg.org>
      auto data = new Data(count);

should be auto data = cast(shared)new Data(count);

Or auto data = new shared(Data)(count);
Aug 12 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 12 Aug 2012 19:57:16 +0200, Jos=C3=A9 Armando Garc=C3=ADa Sancio=
  =

<jsancio gmail.com> wrote:

 On Sun, Aug 12, 2012 at 7:31 AM, Simen Kjaeraas <simen.kjaras gmail.co=

m> =
 wrote:
 On Sun, 12 Aug 2012 09:39:13 +0200, Ivan Trombley  =


 <itrombley dot-borg.org>
      auto data =3D new Data(count);

should be auto data =3D cast(shared)new Data(count);

Or auto data =3D new shared(Data)(count);

I thought I'd tried that. Maybe it was auto data =3D new shared Data(count); -- = Simen
Aug 12 2012
prev sibling parent =?ISO-8859-1?Q?Jos=E9_Armando_Garc=EDa_Sancio?= <jsancio gmail.com> writes:
On Sun, Aug 12, 2012 at 1:18 PM, Simen Kjaeraas <simen.kjaras gmail.com> wr=
ote:
 On Sun, 12 Aug 2012 19:57:16 +0200, Jos=E9 Armando Garc=EDa Sancio
 <jsancio gmail.com> wrote:

 On Sun, Aug 12, 2012 at 7:31 AM, Simen Kjaeraas <simen.kjaras gmail.com>
 wrote:
 On Sun, 12 Aug 2012 09:39:13 +0200, Ivan Trombley
 <itrombley dot-borg.org>
      auto data =3D new Data(count);

should be auto data =3D cast(shared)new Data(count);

Or auto data =3D new shared(Data)(count);

I thought I'd tried that. Maybe it was auto data =3D new shared Data(count);

Someone else may be better able to explain this but "most" D lex token enclose until the end of the line unless de-marked with a pair of (). So I think 'new shared Data(count);' becomes 'new shared(Data(count));'.
Aug 12 2012