Skip to content

Instantly share code, notes, and snippets.

@alexnask
Last active June 19, 2018 17:39
Show Gist options
  • Save alexnask/f2067daf11154b1386fbac092f39c752 to your computer and use it in GitHub Desktop.
Save alexnask/f2067daf11154b1386fbac092f39c752 to your computer and use it in GitHub Desktop.
const TypeInfo = @import("builtin").TypeInfo;
// Generates the following:
// struct { pub const Self = this; data: Base, next: ?*Self, }
pub fn contrived_example(comptime Base: type) type {
comptime {
var fields = []TypeInfo.StructField {
TypeInfo.StructField { .name="data", .offset=null, .field_type=Base },
// SelfType is defined as SelfType = @OpaqueType()
// It's simply a tag type that represents the resulting reified type.
TypeInfo.StructField { .name="next", .offset=null, .field_type=?*TypeInfo.SelfType },
};
var defs = []TypeInfo.Definition {
TypeInfo.Definition { .name="Self", .is_pub=true, .data=TypeInfo.Definition.Data { .Type=TypeInfo.SelfType, }, },
};
var info = TypeInfo.Struct {
.layout=TypeInfo.ContainerLayout.Auto,
.fields=fields[0..],
.defs=defs[0..],
};
return @reify(info);
}
}
test "contrived example" {
const T = contrived_example(u8);
}
// TODO: All TypeInfo slices should be const
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment