www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "is not callable using a non-shared object"

reply Entity325 <lonewolf325 gmail.com> writes:
Usually the DMD compiler errors are very helpful, but I guess 
nothing can be perfect. In this case, I have a class I'm trying 
to declare. The class is intended to be a transport and storage 
medium, to allow information to be passed and updated 
asynchronously between two threads. One of the threads will 
periodically update, add to, or remove from the data. The other 
thread periodically checks the data. Both threads need to be able 
to run asynchronously themselves, so message passing using 
std.concurrency isn't an option. Both threads need to run in a 
loop, so the tools in std.parallelism don't work either.

My problem is: every time I try to declare a shared object in D 
from a non-shared memory space, I get a compiler error: [object] 
is not callable using a non-shared object.

This is really the only bad or ambiguous error warning I've ever 
seen in D. I've spent hours searching for information and gotten 
nothing helpful(most frustrating was the guy who responded to a 
similar inquiry with "Have you checked out std.parallelism and 
std.concurrency?" Cute. Have you tried giving actually helpful 
information instead of being clever?), so I can only assume 
what's going on. Here are the cases that I assume have produced 
the error.
1: attempting to assign a shared object to a non-shared memory 
space(easy to fix)
2: attempting to assign a non-shared object to a shared memory 
space(less easy to fix, but still not bad)
3: attempting to assign a shared object to a shared memory space 
from within a non-shared memory space(no clue)
4: attempting to create a shared object from within a non-shared 
memory space(how am I supposed to create shared objects if I 
can't do it from a non-shared space?)

And this is all assuming that I'm interpreting the error 
correctly. Again, unlike most error messages, this one is neither 
intuitive nor well-documented.


checks and using gross violations of object-oriented design 
philosophy, but I'd rather not do that for what I assume to be 
fairly obvious reasons.

So... basically I've tried everything I can think of to create a 
shared object from an offshoot of the Main function, but all I've 
managed to do is change which line throws the error and just how 
ambiguous the exact syntax that's causing the error is.
Dec 10 2015
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/10/2015 02:07 PM, Entity325 wrote:

 Both threads need to be able to run asynchronously themselves, so message
 passing using std.concurrency isn't an option.
I can still imagine a case where the receiver checks the message queue with a timeout of 0, which is non-blocking.
 My problem is: every time I try to declare a shared object in D from a
 non-shared memory space, I get a compiler error: [object] is not callable
 using a non-shared object.
Instead of "[object]", the message actually contains a member function name, right? If so, here is a reduction: class C { void foo() shared { // <-- note shared } } void main() { auto c = new C(); c.foo(); } Error: shared method deneme.C.foo is not callable using a non-shared object But that's the reverse of what you said: In my case I am constructing a non-shared object. Here is another one where the whole class is shared: shared class C { // <-- note shared void foo() { } } void main() { auto c = new C(); c.foo(); }
 most frustrating was the guy who responded to a similar inquiry with 
"Have
 you checked out std.parallelism and std.concurrency?" Cute.
Perhaps that was me because it is always at the tip of my lips. :) The response is valuable because there are so many problems with data-sharing concurrency.
 1: attempting to assign a shared object to a non-shared memory
 space(easy to fix)
I can't reproduce the same error message with that one.
 2: attempting to assign a non-shared object to a shared memory
 space(less easy to fix, but still not bad)
Ditto.
 3: attempting to assign a shared object to a shared memory space from
 within a non-shared memory space(no clue)
I don't think these are about assignment.
 4: attempting to create a shared object from within a non-shared memory
 space(how am I supposed to create shared objects if I can't do it from a
 non-shared space?)
In this context 'shared' is like 'const'. You can put that attribute to individual member functions, including the constructor: import std.stdio; class C { this(int i) { writefln("Constructing non-shared with %s", i); } this(int i) shared { writefln("Constructing shared with %s", i); } } void main() { auto c = new C(1); auto c2 = new shared(C)(2); } Constructing non-shared with 1 Constructing shared with 2 Ali
Dec 10 2015
prev sibling parent Kagamin <spam here.lot> writes:
On Thursday, 10 December 2015 at 22:07:48 UTC, Entity325 wrote:
 Usually the DMD compiler errors are very helpful, but I guess 
 nothing can be perfect. In this case, I have a class I'm trying 
 to declare. The class is intended to be a transport and storage 
 medium, to allow information to be passed and updated 
 asynchronously between two threads. One of the threads will 
 periodically update, add to, or remove from the data. The other 
 thread periodically checks the data. Both threads need to be 
 able to run asynchronously themselves, so message passing using 
 std.concurrency isn't an option. Both threads need to run in a 
 loop, so the tools in std.parallelism don't work either.
If you can't have either of these two threads own the data, then you can have third "server" thread that will own the data and these two "client" threads will communicate with the server thread through std.concurrency.
Dec 11 2015