www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Runtime struct introspection

reply monkyyy <crazymonkyyy gmail.com> writes:
Rikki suggest it was possible to write a templateless writeln for 
*that* pipe dream.

But when I tried my hand at it; no, when I pull on typeinfo I can 
find all the details of the ast; but when you come to structs, 
its private and designed for the gc.

https://github.com/dlang/dmd/blob/dbf2e20cdf7f8187920f7b357856d9b4f65c8d5e/druntime/src/object.d#L1941


```
     // The xopEquals and xopCmp members are function pointers to 
member
     // functions, which is not guaranteed to share the same ABI, 
as it is not
     // known whether the `this` parameter is the first or second 
argument.
     // This wrapper is to convert it to a delegate which will 
always pass the
     // `this` parameter in the correct way.
     private struct _memberFunc
     {
         union
```

I could probably reverse engineer to get the offsets but I dont 
really want to

---

If walter wants *any* templateless write to work without 
disgusting format strings, you will need at least the layout, and 
typeinfo; if you want the format string to be programmable at 
all, youll need the names.

```d
struct vector2{
   int x;
   int y;
```

`vector2(3,5).runtimeprint;// "vector2(3,5)"`

`vector2(3,5).runtimeprint("($.x,$.y)");// "(3,5)"`

`vector2(3,5).runtimeprint("(x:$.x,y:$.y)");// "(x:3,y:5)"`


---

So we do the song and dance, find the `object.TypeInfo_Struct` 
for vector2

I suggest the following functions is the minimum nessery for 
templateless printing:

```
TypeInfo_Struct t=typeid(vector2);

assert(t.fields==["x","y"]);
assert(t.fieldoffset==[0,int.sizeof]);
assert(t.fieldtype==[typeid(int),typeid(int)]);
```

---

While your in there adding changing the abi I would suggest 
adding the following:

```
enum specialstructs = {none, nullable, sumtype, range}
specialstructs magicnumber; //set by a pragma so theres at least 
an option of attempting to gracefully handle complex types
int delegate(int delegate(void*))* __opApply; //generated if a 
foreachable opApply is defined, otherwise null
bool function(void*)* truthy; //generated if cast(bool) or `bool 
empty` is defined, otherwise null
```
May 26
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 26 May 2026 at 14:56:50 UTC, monkyyy wrote:
 Rikki suggest it was possible to write a templateless writeln 
 for *that* pipe dream.

 ... [] So we do the song and dance, find the 
 `object.TypeInfo_Struct` for vector2

 I suggest the following functions is the minimum nessery for 
 templateless printing:

 ```
 TypeInfo_Struct t=typeid(vector2);

 assert(t.fields==["x","y"]);
 assert(t.fieldoffset==[0,int.sizeof]);
 assert(t.fieldtype==[typeid(int),typeid(int)]);
 ```

 ---
On Tuesday, 26 May 2026 at 14:56:50 UTC, monkyyy wrote:
 Rikki suggest it was possible to write a templateless writeln 
 for *that* pipe dream.

 But when I tried my hand at it; no, when I pull on typeinfo I 
 can find all the details of the ast; but when you come to 
 structs, its private and designed for the gc.

 https://github.com/dlang/dmd/blob/dbf2e20cdf7f8187920f7b357856d9b4f65c8d5e/druntime/src/object.d#L1941
Wow ... I was unaware TypeInfo_Struct did not have members. what do you think about ``` struct TypeInfo_Member { TypeInfo tInfo; string name; size_t offset; } ``` and for `TypeInfo_Class` and `TypeInfo_Struct` as well as `TypeInfo_Enum` to have a `TypeInfo_Member[] members`;
May 29
parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 29 May 2026 at 14:47:59 UTC, Stefan Koch wrote:
 On Tuesday, 26 May 2026 at 14:56:50 UTC, monkyyy wrote:
 Rikki suggest it was possible to write a templateless writeln 
 for *that* pipe dream.

 ... [] So we do the song and dance, find the 
 `object.TypeInfo_Struct` for vector2

 I suggest the following functions is the minimum nessery for 
 templateless printing:

 ```
 TypeInfo_Struct t=typeid(vector2);

 assert(t.fields==["x","y"]);
 assert(t.fieldoffset==[0,int.sizeof]);
 assert(t.fieldtype==[typeid(int),typeid(int)]);
 ```

 ---
On Tuesday, 26 May 2026 at 14:56:50 UTC, monkyyy wrote:
 Rikki suggest it was possible to write a templateless writeln 
 for *that* pipe dream.

 But when I tried my hand at it; no, when I pull on typeinfo I 
 can find all the details of the ast; but when you come to 
 structs, its private and designed for the gc.

 https://github.com/dlang/dmd/blob/dbf2e20cdf7f8187920f7b357856d9b4f65c8d5e/druntime/src/object.d#L1941
Wow ... I was unaware TypeInfo_Struct did not have members. what do you think about ``` struct TypeInfo_Member { TypeInfo tInfo; string name; size_t offset; } ``` and for `TypeInfo_Class` and `TypeInfo_Struct` as well as `TypeInfo_Enum` to have a `TypeInfo_Member[] members`;
Indifferent Im just expecting it to be dumped into memory and soa will be less lines of code and smaller, and maybe semi-compiletime string are complex somehow Traits are far from consistent but you do just need ways to grab data.
May 29