Thread overview
betterC shared static ctor
Jul 21
vit
Jul 21
vit
Jul 21
bauss
July 21

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}

July 21

On Wednesday, 21 July 2021 at 08:02:42 UTC, vit wrote:

>

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}

Sorry, wrong forum section.

July 21

On Wednesday, 21 July 2021 at 08:02:42 UTC, vit wrote:

>

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}

Static module constructors and destructors are not available with betterC unfortunately.

As described here:

https://dlang.org/spec/betterc.html#consequences

July 21

On 7/21/21 4:02 AM, vit wrote:

>

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
     i = 42;
}
extern(C) void main(){
     assert(i != 42);

}

You can use C runtime constructors. However, the compiler doesn't recognize this as a static ctor exactly, so i will not be mutable without casting.

immutable int i;
extern(C):
pragma(crt_constructor) void crt_this() {
    *cast(int *)&i = 42;
}

void main() {
    assert(i == 42);
}

-Steve