Review of std.signal

Johannes Pfau nospam at example.com
Fri Jan 24 01:06:45 PST 2014


Am Fri, 24 Jan 2014 09:52:23 +0100
schrieb Jacob Carlborg <doob at me.com>:

> On 2014-01-23 23:51, Martin Nowak wrote:
> 
> > Wouldn't it be possible to find out whether the delegate context ptr
> > is actually an object? Not sure how to do it safely though and
> > Interfaces slightly differ.
> >
> > ```d
> > import std.stdio;
> >
> > class Foo
> > {
> >      void method() {}
> > }
> >
> > void main()
> > {
> >      auto foo = new Foo;
> >      auto dg = &foo.method;
> >      // It's a class delegate!!!
> >      writeln(cast(void*)foo.classinfo.__vptr, " ",
> > ***cast(void****)dg.ptr, " ", (new ClassInfo).__vptr);
> >      assert(***cast(void****)dg.ptr is (new ClassInfo).__vptr);
> > }
> > ```
> 
> Why don't you just cast the delegate context pointer to Object? Like
> this:
> 
> auto result = cast(Object) dg.ptr;
> 
> If "result" is not null the context pointer points to an object. 
> Although this won't handled interfaces. I consider it a bug that an 
> interface cannot be casted to Object.
> 

A delegate context could be a struct or closure as well, then calling
cast(Object) is invalid and may crash.
There's no good solution, the old std.signals just assumes all
delegates belong to classes IIRC. 

Why do we even need this auto-detection? Just add an additional
connectWeak function which either just takes a normal delegate and
require that all delegates passed to it have objects as context. Or
better: add a proper WeakRef type to druntime/GC as discussed before,
then use overloads.


More information about the Digitalmars-d mailing list