Bake structs

You can’t directly bake a struct at compile time; the closest you can get is to compute the struct then stash its memory representation, and then retrieve it at runtime:

#import "Basic";
 
 
Foo :: struct {
    x : int;
}
 
foo : Foo;
foo_data :: #run bake_as_u8(make_struct());
 
make_struct :: () -> Foo {
    // compute a struct
    return Foo.{12};
}

bake_as_u8 :: (value : $T) -> [] u8 {
    array : [size_of(T)] u8;
    memcpy(*array, *value, size_of(T));
    return array;
}
 
restore_from_u8 :: (dest: *$T, data: [] u8) {
    value : T;
    memcpy(dest, data.data, size_of(T));
}
 
init :: () {
    restore_from_u8(*foo, foo_data);
}
 
main :: () {
    init();
    print("% % %\n", foo, foo.x, type_of(foo));
}