#@typeInfo of type alias?

1 messages · Page 1 of 1 (latest)

magic brook
#

Is there a way to get the name of a type alias at comptime? E.g.:

const std = @import("std");

const Date = []const u8;

const Body = struct {
    a: i32,
    date: Date,
};

pub fn main() !void {
    inline for (@typeInfo(Body).Struct.fields) |field| {
        std.debug.print("{}\n", .{field.type});
    }
}

The above prints "i32" and "[]const u8" which is correct, but is there any way I could use a type alias (or other means) to know date is intended to be a Date type?

foggy flint
#

You need to wrap it to a struct

magic brook
#

Hmm there's no other way?

foggy flint
#

Nope

magic brook
#

Maybe I could trace the Body type back to its source file and walk the AST to find out the real type, as Date

#

Just a big hassle

foggy flint
#

what are you trying to do

magic brook
#

Currently, date validation on json objects (REST api stuff)

foggy flint
#

I'd either wrap the date into own struct, or provide a schema

magic brook
#

Provide a schema?

foggy flint
#

parse / validate the contents based on schema struct

magic brook
#

I'd have to somehow associate these schemas with each struct 🤔

#

just all feels very cumbersome

severe crag