Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uniquely identify a user defined type in D?

I need to generate something that can be used as a unique handle for a user defined type (struct or class) in the D programming language. Preferably this would be a compile time computable value. I want the handle to relate to the name of the type as well as change if the internal structure (data layout) of the type changes but remain the same for most other edits (including compiling the same type into a different app).

This is not a security thing so it doesn't need to be hard to bypass or anything

My current thought is to use a string with something like an MD5 hash of the type name and member types and names.

Any thoughts

like image 809
BCS Avatar asked Dec 02 '25 05:12

BCS


1 Answers

After thinking about this for a bit, I think this would be a feasible approach (note: this is just pseudo code):

UniqueId(Type) = Type.stringof ~ MemberIds!(Type.tupleof)

UniqueId(Type) if( Type is builtin ) = Type.stringof

MemberIds(M, Ms...) = "," ~ UniqueId!(typeof(M))
                      ~ "@" ~ ToString!(M.offsetof)
                      ~ ":" ~ M.stringof
                      ~ MemberIds!(Ms)

That is, construct the unique ID from the type's name (you might need to chop off the module and package, not sure), and each member's type's ID, offset and name.

Depending on exactly what you want, you could remove the member name.

like image 167
DK. Avatar answered Dec 04 '25 10:12

DK.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!