#How can I prepare the value in comptime and use it in runtime?

1 messages · Page 1 of 1 (latest)

true anchor
#
comptime var query: [] const u8 = "";

keen turret
#

you can’t
the best you can do is make it part of the type like

fn foo(comptime query: []const u8) type {
    return struct {
        pub var query = query
        // …
    };
}
true anchor
#

How can I prepare the value in comptime and use it in runtime?

#

I need to create a value at compile time and ensure it isn’t modified at runtime.

ionic hill
true anchor
#

Something like this

pub const sample = struct {
    var query: []const u8 = "";

    inline fn addSelect() type {
        query = query ++ "select";
        return @This();
    }
    inline fn addFrom() type {
        query = query ++ "from";
        return @This();
    }
};
ionic hill
#

I think simple string building like that can be done at comptime. I can give it a go later unless someone else does before me

#

Not sure though about being methods of a struct

keen turret
#

you wouldnt be able to do that
top-level comptime variables cant be mutated after the assignment, only function-local comptime variables can be

ionic hill
keen turret
#

no, this has been a rule for a while

#

way before then

#

afaik that change only affects pointers to comptime vars

novel elk
#

You'll have to create a new instance of sample every time you want to concat something to the string like this:

pub const Query = struct {
    query_string: []const u8 = "",

    pub fn empty() Query {
        return .{};
    }

    pub fn addSelect(self: Query) Query {
        return Query{ .query_string = self.query_string ++ "select" };
    }

    pub fn addFrom(self: Query) Query {
        return Query{ .query_string = self.query_string ++ "from" };
    }
};

pub fn main() !void {
    const query = comptime Query.empty().addSelect().addFrom();

    std.log.info("{s}", .{query.query_string});
}

Not sure how it affects compiler performances

keen turret
#

yeah thats a good way to do it

novel elk
#

Then you can use the string as the format string for a writer / print function

true anchor
novel elk
#

Not the best but it works

pub const Query = struct {
    // ...

    pub fn addTableName(self: Query) Query {
        return Query{ .query_string = self.query_string ++ " {s} " };
    }

    pub fn addColumnName(self: Query) Query {
        return Query{ .query_string = self.query_string ++ " {s} " };
    }
    // ...
};

pub fn main() !void {
    const query = comptime make_query().addSelect().addColumnName().addFrom().addTableName();

    std.log.info(query.query_string, .{ "names", "users" });
}
true anchor
#

This is what I'm trying to achieve but this is impossible in zig.


const str = struct {
    const Self = *const @This();
    var v: type = struct {
        const query = "";
    };
    inline fn append(self: Self, comptime _str: []const u8, _: []const u8) Self {
        comptime {
            v = struct {
                const query = v.query ++ _str;
            };
        }
        return self;
    }
};

test str {
    const sample = str{};
    _ = sample.append("test", "asdf");
    @compileLog(@TypeOf(sample).v.query);
}
novel elk
#

All the non-comptime parameters are strings or can they be other types ?

true anchor
#

Yep, it will be anytype.

novel elk
#

If they are anytype, it will be much easier to build the string at runtime

true anchor
#

Yeah, that makes it way easier.