www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Are interfaces supported in Better C mode?

reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
Reading the docs on Better C mode I got the impression that it 
should be possible to implement an interface in a struct. Is this 
not the case?

I tried extern (C++) too, but I get an error.

extern (C++) interface A {
     void sayHello();
}

extern (C++) struct B : A {

}
Nov 14 2020
next sibling parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Saturday, 14 November 2020 at 21:09:59 UTC, Dibyendu Majumdar 
wrote:
 Reading the docs on Better C mode I got the impression that it 
 should be possible to implement an interface in a struct. Is 
 this not the case?
I guess it needs to be class? extern (C++) interface A { void sayHello(); } extern (C++) class B : A { void sayHello() {} } Above seems to compile.
Nov 14 2020
parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Saturday, 14 November 2020 at 21:40:31 UTC, Dibyendu Majumdar 
wrote:
 On Saturday, 14 November 2020 at 21:09:59 UTC, Dibyendu 
 Majumdar wrote:
 Reading the docs on Better C mode I got the impression that it 
 should be possible to implement an interface in a struct. Is 
 this not the case?
I guess it needs to be class?
Well this compiles but doesn't link. import core.stdc.stdio : printf; extern (C++) interface A { void sayHello(); } extern (C++) class B : A { void sayHello() { printf("hello\n"); } } extern (C) void main() { B b; b.sayHello(); } /opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/ld: interfaces.o:(.data._D10interfaces1A11__InterfaceZ+0x0): undefined reference to `_D14TypeInfo_Class6__vtblZ' /opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/ld: interfaces.o:(.data._D10interfaces1B6__initZ+0x8): undefined reference to `_D10interfaces1B7__ClassZ'
Nov 14 2020
parent reply kinke <noone nowhere.com> writes:
On Saturday, 14 November 2020 at 21:46:38 UTC, Dibyendu Majumdar 
wrote:
 Well this compiles but doesn't link.
This works with LDC: import core.stdc.stdio : printf; extern (C++) interface A { void sayHello(); } extern (C++) class B : A { void sayHello() { printf("hello\n"); } } extern (C) void main() { scope b = new B; b.sayHello(); }
Nov 14 2020
next sibling parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Sunday, 15 November 2020 at 01:00:27 UTC, kinke wrote:
 On Saturday, 14 November 2020 at 21:46:38 UTC, Dibyendu 
 Majumdar wrote:
 Well this compiles but doesn't link.
This works with LDC: import core.stdc.stdio : printf; extern (C++) interface A { void sayHello(); } extern (C++) class B : A { void sayHello() { printf("hello\n"); } } extern (C) void main() { scope b = new B; b.sayHello(); }
Hi, I downloaded ldc2 1.24 and tried this out. It compiled and linked okay - but crashes with segmentation fault on the line that invokes the sayHello() method. I am on RHEL 7.9. Regards
Nov 14 2020
prev sibling parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Sunday, 15 November 2020 at 01:00:27 UTC, kinke wrote:
 On Saturday, 14 November 2020 at 21:46:38 UTC, Dibyendu 
 Majumdar wrote:
 Well this compiles but doesn't link.
This works with LDC: extern (C) void main() { scope b = new B; b.sayHello(); }
I thought new isn't supported in better C mode ?
Nov 14 2020
parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 15 November 2020 at 01:30:24 UTC, Dibyendu Majumdar 
wrote:
 On Sunday, 15 November 2020 at 01:00:27 UTC, kinke wrote:
 On Saturday, 14 November 2020 at 21:46:38 UTC, Dibyendu 
 Majumdar wrote:
 Well this compiles but doesn't link.
This works with LDC: extern (C) void main() { scope b = new B; b.sayHello(); }
I thought new isn't supported in better C mode ?
`scope` causes the class instance to be allocated on the stack. Source: https://dlang.org/spec/expression.html#new_expressions
Nov 14 2020
parent Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Sunday, 15 November 2020 at 01:43:00 UTC, Paul Backus wrote:
 On Sunday, 15 November 2020 at 01:30:24 UTC, Dibyendu Majumdar
 I thought new isn't supported in better C mode ?
`scope` causes the class instance to be allocated on the stack. Source: https://dlang.org/spec/expression.html#new_expressions
Okay thank you - I didn't know that.
Nov 14 2020
prev sibling parent reply Dibyendu Majumdar <mobile majumdar.org.uk> writes:
On Saturday, 14 November 2020 at 21:09:59 UTC, Dibyendu Majumdar 
wrote:
 Reading the docs on Better C mode I got the impression that it 
 should be possible to implement an interface in a struct. Is 
 this not the case?
https://issues.dlang.org/show_bug.cgi?id=21412
Nov 21 2020
parent Dibyendu Majumdar <mobile majumdar.org.uk> writes:
On Saturday, 21 November 2020 at 23:48:06 UTC, Dibyendu Majumdar 
wrote:
 On Saturday, 14 November 2020 at 21:09:59 UTC, Dibyendu 
 Majumdar wrote:
 Reading the docs on Better C mode I got the impression that it 
 should be possible to implement an interface in a struct. Is 
 this not the case?
https://issues.dlang.org/show_bug.cgi?id=21412
Looks like this works with DMD and LDC: import core.stdc.stdio : printf; extern (C++) abstract class A { void sayHello(); } extern (C++) class B : A { override void sayHello() { printf("hello\n"); } } extern (C) void main() { scope b = new B; b.sayHello(); } So - replacing 'interface' with 'abstract class' solved it. Also need to use 'new' to allocate. Compiler doesn't complain about the null value.
Nov 21 2020