Thread overview
use delegate with IUnknown
Oct 20, 2008
Long Chang
Oct 20, 2008
Sergey Gromov
Oct 20, 2008
ore-sama
Oct 20, 2008
John C
October 20, 2008
I compile this code got error....

   1. interface IUnknown{}
   2. class klass : IUnknown{
   3.     this(){}
   4.     void add(void delegate() dg){}
   5. }
   6. void main(){
   7.     auto t  = new klass;
   8.     t.add(delegate void(){});
   9. }

t2.d(8): function t2.klass.add (void delegate()) does not match parameter types (void delegate())
t2.d(8): Error: cannot implicitly convert expression (__dgliteral1) of type void delegate() to void delegate()


this code work fine.
   1. interface IUnknown_{}
   2. class klass : IUnknown_{
   3.     this(){}
   4.     void add(void delegate() dg){}
   5. }
   6. void main(){
   7.     auto t  = new klass;
   8.     t.add(delegate void(){});
   9. }


Is this a bug?
October 20, 2008
Mon, 20 Oct 2008 06:53:39 -0400,
Long Chang wrote:
> I compile this code got error....
> 
>    1. interface IUnknown{}
>    2. class klass : IUnknown{
>    3.     this(){}
>    4.     void add(void delegate() dg){}
>    5. }
>    6. void main(){
>    7.     auto t  = new klass;
>    8.     t.add(delegate void(){});
>    9. }
> 
> t2.d(8): function t2.klass.add (void delegate()) does not match parameter types (void delegate())
> t2.d(8): Error: cannot implicitly convert expression (__dgliteral1) of type void delegate() to void delegate()
> 
> Is this a bug?

IUnknown is a built-in, deeply hacked interface which allows to write proper COM objects in pure D.  You must import it from std.c.windows.com module.  Obviously it applies some restrictions on methods which can be defined there.  Your snipped starts to compile if you replace line 4 with

    extern (Windows) void foo() {}
    t.add(&foo);
October 20, 2008
Long Chang Wrote:

> I compile this code got error....
> 
>    1. interface IUnknown{}
>    2. class klass : IUnknown{
>    3.     this(){}
>    4.     void add(void delegate() dg){}
>    5. }
>    6. void main(){
>    7.     auto t  = new klass;
>    8.     t.add(delegate void(){});
>    9. }
> 
> t2.d(8): function t2.klass.add (void delegate()) does not match parameter types (void delegate())
> t2.d(8): Error: cannot implicitly convert expression (__dgliteral1) of type void delegate() to void delegate()
> 
> 
> this code work fine.
>    1. interface IUnknown_{}
>    2. class klass : IUnknown_{
>    3.     this(){}
>    4.     void add(void delegate() dg){}
>    5. }
>    6. void main(){
>    7.     auto t  = new klass;
>    8.     t.add(delegate void(){});
>    9. }
> 
> 
> Is this a bug?

No. The compiler treats any interface named IUnknown specially - including modifying the calling convention, so you'll need to add extern(D) on your class.

IUnknown is intended for use with COM anyway.
October 20, 2008
Sergey Gromov Wrote:

> IUnknown is a built-in, deeply hacked interface which allows to write proper COM objects in pure D.  You must import it from std.c.windows.com module.  Obviously it applies some restrictions on methods which can be defined there.  Your snipped starts to compile if you replace line 4 with
> 
>     extern (Windows) void foo() {}
>     t.add(&foo);

so this is a bug: compiler doesn't provide important info that calling conventions of argument and parameter are incompatible.