-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: Remove the ambiguity of forced comptime types #5672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Better error messages are always a good thing, but a function returning a comptime-only type already implies it can only be called at comptime, and it doesn't take much investigation to find any violations. That's just how the language works. I don't think being even more explicit about something you need to understand to use the language anyway is worth the extra verbosity. Furthermore, this would introduce a distinct divide between comptime and runtime code, against the aims of itself. |
I want to make not distinction in comptime and runtime code const std = @import("std");
const Dynamic = struct { value: var = undefined, };
fn getValue(num: u64, msg: []const u8) Dynamic {
var dynamic = Dynamic {};
dynamic.value = true;
dynamic.value = num;
dynamic.value = msg;
return dynamic;
}
pub fn main() !void {
std.debug.warn("{}\n", .{ getValue(3,"hello world").value });
} if you copy the body of const std = @import("std");
const Dynamic = struct { value: var = undefined, };
pub fn main() !void {
comptime var dynamic = Dynamic {};
dynamic.value = true;
dynamic.value = 3;
dynamic.value = "hello world";
std.debug.warn("{}\n", .{ dynamic.value });
} You need to add comptime in this case because I want the compiler to force the use of some comptime notation ( click to expand to the proposal solutionconst std = @import("std");
const Dynamic = struct { value: var = undefined, };
fn getValue(comptime num: u64, comptime msg: []const u8) Dynamic {
comptime var dynamic = Dynamic {};
dynamic.value = true;
dynamic.value = num;
dynamic.value = msg;
return dynamic;
}
pub fn main() !void {
std.debug.warn("{}\n", .{ getValue(3,"hello world").value });
}
|
My bad, misread the code. The perceived semantics mismatch was an artifact of that. However, I'm still against this. Sometimes being explicit just gets in the way, like it is with |
How would this interact with functions where the return type is generic (that is, computed at comptime), and sometimes is a comptime-forced type? Would it be better or worse? |
The principal reason of this proposals is to preserve the symmetry between compiled and interpreted code.
comptime
to the arguments of a function that returns a forced comptime type.var
and forcedcomptime var
.Forced comptime type
Definition
I doesn't know how is called that types; but I refer to types such that:
T
is a forced comptime type iffThrow the compiler error:
Examples
For example,
type
is a forced comptime type, becauseAn other example,
Dynamic
is a forced comptime type:Force to add
comptime
to the arguments of a function that returns a forced comptime typeA function that returns a forced comptime type, only can be called with constants arguments. So it makes sense to force the arguments to be
comptime
.For example, here,
ConstantType
is a function that returns a forced comptime type; in this case,type
.As expected, shows an error; but the error message doesn't explain the reason behind. So the solution is to change the error message to show that the return type of the function requires that the arguments need to be constants.
The proposal is to change, for example, to
Remove the ambiguity between
var
and forcedcomptime var
The problem
By definition, a forced comptime type throws an error in a compiled function:
But, in a interpreted only functions, doesn't:
For example,
The reason is that
var
in theinterpretedOnlyFunction
is implicitly considered as a comptime variable.Extra
To be much clear that
var
andcomptime var
generates different results:(#868 (comment))
Proposal
So, the proposal is to create
comptime var
as a token to avoid generate different code with respect a runtime value; considervar
of a forced compiled type an error and change the error message, then the only way is to declared asconst
orcomptime var
.This makes more clear the intention and preserve the symmetry between compiled and interpreted code.
Examples:
Maybe
comptime var
can be used as a hint. For example,(Note: this last example need to be discussed in a dedicated thread because the reason of the difference is the lazy way that if is evaluated. Remark that the main reason of this proposal is to force the use of
comptime var
for forced comptime types)The text was updated successfully, but these errors were encountered: