www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - use delegate with IUnknown

reply Long Chang <changlon gmail.com> writes:
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?
Oct 20 2008
next sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
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);
Oct 20 2008
parent ore-sama <spam here.lot> writes:
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.
Oct 20 2008
prev sibling parent John C <johnch_atms hotmail.com> writes:
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.
Oct 20 2008