Thread overview
Are interfaces supported in Better C mode?
Nov 15
kinke
November 14
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 {

}


November 14
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.


November 14
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'


November 15
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();
}
November 15
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
November 15
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 ?


November 15
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
November 15
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.
3 days ago
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

3 days ago
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.