Thread overview
betterC and std.containers don't work together?
Sep 19, 2023
Ki Rill
Sep 19, 2023
Jonathan M Davis
September 19, 2023

I've been trying out std containers. Checked Phobos code - all seems to be @nogc. Good. Then I try out this and it fails because something throws in array construction and that's not available in betterC: https://run.dlang.io/is/WAjVuG

extern(C):
import core.stdc.stdio;
import std.container;

void main()
{
    printf("hello, world\n");
    Array!int ai;
    ai ~= 1;
}

/* Compiler output:
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/array/construction.d(207): Error: cannot use try-catch statements with -betterC
/dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(151): Error: template instance `core.internal.array.construction._d_arraysetctor!(const(Array!int)[], const(Array!int))` error instantiating
/dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(244):        instantiated from here: `RangeT!(const(Array!int))`
/dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(633):        instantiated from here: `RangeT!(Array!int)`
onlineapp.d(8):        instantiated from here: `Array!int`
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/array/construction.d(207): Error: cannot use try-catch statements with -betterC
/dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(151): Error: template instance `core.internal.array.construction._d_arraysetctor!(immutable(Array!int)[], immutable(Array!int))` error instantiating
/dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(639):        instantiated from here: `RangeT!(immutable(Array!int))`
onlineapp.d(8):        instantiated from here: `Array!int`
*/
September 19, 2023
Yeah that is to be expected, things working in -betterC from druntime/phobos is the exception not the rule.

Due to assumptions about what is available.
September 18, 2023
On Monday, September 18, 2023 10:02:00 PM MDT Ki Rill via Digitalmars-d wrote:
> I've been trying out std containers. Checked Phobos code - all seems to be @nogc. Good. Then I try out this and it fails because something throws in array construction and that's not available in betterC: https://run.dlang.io/is/WAjVuG
>
> ```D
> extern(C):
> import core.stdc.stdio;
> import std.container;
>
> void main()
> {
>      printf("hello, world\n");
>      Array!int ai;
>      ai ~= 1;
> }
>
> /* Compiler output:
> /dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/array/constru
> ction.d(207): Error: cannot use try-catch statements with -betterC
> /dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(151): Error:
> template instance
> `core.internal.array.construction._d_arraysetctor!(const(Array!int)[],
> const(Array!int))` error instantiating
> /dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(244):
> instantiated from here: `RangeT!(const(Array!int))`
> /dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(633):
> instantiated from here: `RangeT!(Array!int)` onlineapp.d(8):
> instantiated from here: `Array!int`
> /dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/array/constru
> ction.d(207): Error: cannot use try-catch statements with -betterC
> /dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(151): Error:
> template instance
> `core.internal.array.construction._d_arraysetctor!(immutable(Array!int)[],
> immutable(Array!int))` error instantiating
> /dlang/dmd/linux/bin64/../../src/phobos/std/container/array.d(639):
> instantiated from here: `RangeT!(immutable(Array!int))` onlineapp.d(8):
>    instantiated from here: `Array!int`
> */
> ```

-betterC is really only intended to make it easier to port C code to D, and it naturally reduces how much of D that you can use, because it involves not using D's runtime.

On the other hand, Phobos in general has been designed with the idea that you will be using the entire language. Some parts of it happen to work with -betterC, but pretty much all of it was written before -betterC was even a thing, so if it works with -betterC, it's basically by accident.

- Jonathan M Davis