Thread overview
By-ref opaque structs

14 hours ago

Atila Neves

Sep 06

Ogion

July 30

By-ref opaque structs
Gravatar of Richard (Rikki) Andrew CattermolePosted by Richard (Rikki) Andrew CattermolePermalinkReply
Gravatar of Richard (Rikki) Andrew Cattermole

Opaque structs are typically used as pointers.

struct Context;

extern(C) void doSomething(Context*);

However they can be useful for payloads in D with dynamically sized structs.

struct Payload;

struct Node {
    Node* next;
    Payload[0] payload;
}

void walk(Node* node) {
    Payload* payload = &node.payload[0];
}

So let's make this a little easier on ourselves, and make opaque structs always by-ref if it is were otherwise by-value (and then error) and allow them to be the last field of a struct.

struct Node {
    Node* next;
    Payload payload;
}

void view(Payload payload) {
    Payload* payload2 = &payload;
}

This has a nice usage in sumtypes:

struct None;

__sumtype(None) sumtype;
sumtype.match((a) => 2);

As it means we don't have to link against or use random None's all over codebases that are not linking against druntime, but we can still define it in object.d

September 01

Re: By-ref opaque structs
Gravatar of Paul BackusPosted by Paul Backus
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Paul Backus

Gravatar of Paul Backus

Posted in reply to Richard (Rikki) Andrew Cattermole

On Thursday, 30 July 2026 at 01:18:26 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

So let's make this a little easier on ourselves, and make opaque structs always by-ref if it is were otherwise by-value (and then error) and allow them to be the last field of a struct.

struct Node {
    Node* next;
    Payload payload;
}

void view(Payload payload) {
    Payload* payload2 = &payload;
}

Why not simply use Payload[0] and ref Payload payload to clearly communicate to the reader what the code is doing? I don't see the benefit of adding a special case here.

September 06

Re: By-ref opaque structs
Gravatar of OgionPosted by Ogion
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Ogion

Gravatar of Ogion

Posted in reply to Richard (Rikki) Andrew Cattermole

On Thursday, 30 July 2026 at 01:18:26 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

This has a nice usage in sumtypes:

struct None;

__sumtype(None) sumtype;
sumtype.match((a) => 2);

As it means we don't have to link against or use random None's all over codebases that are not linking against druntime, but we can still define it in object.d

I use typeof(null) exactly for this:

SumType!(typeof(null)) sumtype;
writeln(sumtype.match!((s) => 42));

September 23

Re: By-ref opaque structs
Gravatar of Quirin SchrollPosted by Quirin Schroll
in reply to Richard (Rikki) Andrew Cattermole
PermalinkReply

Quirin Schroll

Gravatar of Quirin Schroll

Posted in reply to Richard (Rikki) Andrew Cattermole

On Thursday, 30 July 2026 at 01:18:26 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

Opaque structs are typically used as pointers.

struct Context;

extern(C) void doSomething(Context*);

However they can be useful for payloads in D with dynamically sized structs.

struct Payload;

struct Node {
    Node* next;
    Payload[0] payload;
}

void walk(Node* node) {
    Payload* payload = &node.payload[0];
}

If anything, we should make the practice of using a 0-size array obsolete and use an attribute or some other indicator for such a dynamically sized payload array. Indexing a 0-size array just looks wrong. Ideally, it would be something like Payload[?].

14 hours ago

Re: By-ref opaque structs
Gravatar of Atila NevesPosted by Atila Neves
in reply to Paul Backus
PermalinkReply

Atila Neves

Gravatar of Atila Neves

Posted in reply to Paul Backus

On Tuesday, 1 September 2026 at 20:30:13 UTC, Paul Backus wrote:

>

On Thursday, 30 July 2026 at 01:18:26 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

So let's make this a little easier on ourselves, and make opaque structs always by-ref if it is were otherwise by-value (and then error) and allow them to be the last field of a struct.

struct Node {
    Node* next;
    Payload payload;
}

void view(Payload payload) {
    Payload* payload2 = &payload;
}

Why not simply use Payload[0] and ref Payload payload to clearly communicate to the reader what the code is doing? I don't see the benefit of adding a special case here.

This.