#UUIDs for Types

1 messages · Page 1 of 1 (latest)

queen shoal
#

I am using allot of virtual functions in a gui system and i want to be able to check "types",
For example:

// @TypeOf(node) == *Node
if (node.type_id == @idOfType(ClockNode)) {
  clock: *ClockNode = @ptrCast(node);
  return clock.getTime();
}```
Does zig store anything like this? using strings wouldnt be ideal and the other option is enums which is more code and less scaleable
viscid trellis
#

you can get a type id esc system with this:

pub fn typeId(comptime T: type) *const anyopaque {
    const storage = struct {
        var v: u8 = 0;

        comptime {
            _ = T;
        }
    };

    return &storage.v;
}

(EDITED)

queen shoal
#

wow thats pretty smart, ill use this if theres no other way. I would think the compiler stores IDs already but its not accessible

viscid trellis
#

i think theres an issue for this, someone got the same result by doing something funky with anyerror but i dont remember what lol

queen shoal
#

actually a ct hashing function would also work,

#

but with the above at least theres no collisions

bleak monolith
viscid trellis
queen shoal
#

the coolest version would be if it checked for duplicates, if we had const X = f32 and const Y = f32 they would have the same IDs, and same with structs that have identical layouts

viscid trellis
#

oh he made it into a library lol

viscid trellis
viscid trellis
#

it do

queen shoal
#

oh.

#

i guess thats an obvious optimisation if X == Y is allready readilly checkable

viscid trellis
#

it works at comptime/runtime, so its not really that much different from a builtin

#

only downside is no @intFromPtr at comptime of course

viscid trellis
#

polluting anyerror (more than it already is) just feels wrong to me tho lol

queen shoal
#

all of this feels wrong.

viscid trellis
#

how so?

queen shoal
#

its creating a struct, eitherway the compiler keeps track of it

#

or does it get disposed because its not returned?

viscid trellis
#

the only thing that gets added to the binary is the 0

queen shoal
#

i was wondering about the struct being created at comptime

viscid trellis
viscid trellis
queen shoal
#

what bug?

#

reusing the same pointer?

viscid trellis
#

yea

glacial sleet
#

has to be var v: u8 = 0; to prevent merging

viscid trellis
#

ah thanks

queen shoal
viscid trellis
queen shoal
#

probably not lol, just none of this would be needed if the proposal gets approved