#How can I prepare the value in comptime and use it in runtime?
1 messages · Page 1 of 1 (latest)
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
// …
};
}
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.
Can you elaborate on your use case more? When you say you want to "prepare the value" what do you mean? What kind of preparations are you doing?
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();
}
};
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
you wouldnt be able to do that
top-level comptime variables cant be mutated after the assignment, only function-local comptime variables can be
Is this related to the 'comptime var` change done a while ago?
no, this has been a rule for a while
way before then
afaik that change only affects pointers to comptime vars
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
yeah thats a good way to do it
Then you can use the string as the format string for a writer / print function
What if one of the parameters is only available at runtime?
Never mind, I'll just use an allocator for this one. Thanks, though!
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" });
}
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);
}
All the non-comptime parameters are strings or can they be other types ?
Yep, it will be anytype.
If they are anytype, it will be much easier to build the string at runtime
Yeah, that makes it way easier.