digitalmars.dip.ideas - By-ref opaque structs
- Richard (Rikki) Andrew Cattermole (38/38) Jul 29 Opaque structs are typically used as pointers.
Opaque structs are typically used as pointers.
```d
struct Context;
extern(C) void doSomething(Context*);
```
However they can be useful for payloads in D with dynamically
sized structs.
```d
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.
```d
struct Node {
Node* next;
Payload payload;
}
void view(Payload payload) {
Payload* payload2 = &payload;
}
```
This has a nice usage in sumtypes:
```d
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
Jul 29







Richard (Rikki) Andrew Cattermole <richard cattermole.co.nz>