Overriden method not detected ?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 3 14:04:41 PDT 2016


On 06/03/2016 10:06 PM, chmike wrote:
> If I have a static immutable object, I don't have to declare it as
> shared because it is implicit. Right ?

Right.

[...]
> 1. question
> -----------
> I had a __gshared Info[N] infos_; that I fill in a static this() method.

Should be filling it in a `shared static this`, I think. With just 
`static this`, the fill code is run once per thread.

> How should I declare this with shared ? Is it enough to write static
> shared Info[N] infos; ? Since shared is transitive, will the info
> objects be implicitly shared ?

The variable `infos` and its elements are shared then, yes.

When you're interested in the type of some expression, you can use 
pragma(msg, ...) to print it during compilation. For example:

     pragma(msg, typeof(infos[0])); /* Should print "shared(Info)". */

> 2. question
> -----------
> As I said above, I instantiate my Info objects in a private static
> this() method. The objects are emplaced in a static shared void array.
> Since emplace only accept a void[] as argument I had to cast away the
> shared as you did in your example. The compiler seams happy.

As far as I know, that's often the way to treat shared. Cast it away 
when you know it's safe.

> However I didn't synchronized that code because I assume it is executed
> by the main thread before main starts. There is thus no risk of race
> conditions. Is this assumption correct ?

Use `shared static this` as mentioned.

> 3. error
> --------
> A weird error is that there is apparently no overload for opEquals. Do I
> have to define one my self ? Is this so that users can synchronize
> themselves if needed ?
> source/app.d(9,13): Error: none of the overloads of 'opEquals' are
> callable using argument types (shared(Info), shared(Info)), candidates are:
> /usr/include/dmd/druntime/import/object.d(143,6): object.opEquals(Object
> lhs, Object rhs)
> /usr/include/dmd/druntime/import/object.d(168,6):
> object.opEquals(const(Object) lhs, const(Object) rhs)
> I guess I have to define my own opEqual.

Well, there doesn't seem to be an equality function for shared class 
objects. I'm not sure what the difficulties are in providing one.

In the meantime, you can either do your own equality thing without the 
`==` operator, or assure thread-safety yourself and cast shared away.

> 4. error
> --------
> Another error is with writeln(info); where info is now a shared(Info).
>
> /usr/include/dmd/phobos/std/format.d(2904,5): Error: static assert
> "unable to format shared objects"
> /usr/include/dmd/phobos/std/format.d(3477,16): instantiated from here:
> formatValue!(LockingTextWriter, shared(Info), char)
> /usr/include/dmd/phobos/std/format.d(467,54):        instantiated from
> here: formatGeneric!(LockingTextWriter, shared(Info), char)
> /usr/include/dmd/phobos/std/stdio.d(1316,31):        instantiated from
> here: formattedWrite!(LockingTextWriter, char, shared(Info))
> /usr/include/dmd/phobos/std/stdio.d(3114,28):        instantiated from
> here: write!(shared(Info), char)
> source/app.d(12,12):        instantiated from here: writeln!(shared(Info))
>
> How can I solve that error ?

writeln isn't thread-safe, I guess. Synchronize and cast shared away, I 
suppose.


More information about the Digitalmars-d-learn mailing list