shared: Has anyone used it without a lot of pain?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 4 16:40:57 PDT 2017


On Tuesday, April 04, 2017 21:56:37 Atila Neves via Digitalmars-d wrote:
> I feel dirty if I write `__gshared`. I sneeze when I read it.

Well, it was designed with C global variables in mind, and it's pretty risky
to use it for anything else, though you can get away with it if you're
careful. However, I'm inclined to argue that __gshared really shouldn't ever
be used on anything that's extern(D). The problem of course is that shared
is a bit of a pain to use. Some of those pain points make sense even if
they're annoying and some of them should probably be fixed.

> But everytime I try and use `shared` I get trouble for it.

Yeah. It works, but it is annoying to use.

> The reason why what I was trying to do isn't possible is obvious
> in hindsight, but it's still annoying. So either code duplication
> or mixins, huh?

Unfortunately, the idea seems to be that any user-defined objects that are
marked as shared should have been designed to be shared and that a type
would normally either always be shared or never shared. On some level, that
makes sense, but it can also be really annoying, because sometimes, it
really does make sense to use the same type in both shared and un-shared
contexts. But the reality of the matter is that anything that isn't really
basic is going to tend to have to either always be shared or never shared.
The destructor case does seem like it should be fixed though.

- Jonathan M Davis



More information about the Digitalmars-d mailing list