Currently, the compiler (like @typeName) writes it `fn(...) Type` but zig fmt writes it `fn (...) Type` (notice the space after `fn`). This inconsistency is now resolved and function types are consistently written the zig fmt way. Before this there were more `fn (...) Type` occurrences than `fn(...) Type` already.
51 lines
1.4 KiB
Zig
51 lines
1.4 KiB
Zig
const S = struct {
|
|
fnPtr: fn () void,
|
|
};
|
|
fn bar() void {}
|
|
fn baz() void {}
|
|
var runtime: bool = true;
|
|
fn ifExpr() S {
|
|
if (runtime) {
|
|
return .{
|
|
.fnPtr = bar,
|
|
};
|
|
} else {
|
|
return .{
|
|
.fnPtr = baz,
|
|
};
|
|
}
|
|
}
|
|
pub export fn entry1() void {
|
|
_ = ifExpr();
|
|
}
|
|
fn switchExpr() S {
|
|
switch (runtime) {
|
|
true => return .{
|
|
.fnPtr = bar,
|
|
},
|
|
false => return .{
|
|
.fnPtr = baz,
|
|
},
|
|
}
|
|
}
|
|
pub export fn entry2() void {
|
|
_ = switchExpr();
|
|
}
|
|
|
|
// error
|
|
// backend=stage2
|
|
// target=native
|
|
//
|
|
// :8:9: error: unable to resolve comptime value
|
|
// :8:9: note: condition in comptime branch must be comptime-known
|
|
// :7:13: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
|
|
// :2:12: note: struct requires comptime because of this field
|
|
// :2:12: note: use '*const fn () void' for a function pointer type
|
|
// :19:15: note: called from here
|
|
// :22:13: error: unable to resolve comptime value
|
|
// :22:13: note: condition in comptime switch must be comptime-known
|
|
// :21:17: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
|
|
// :2:12: note: struct requires comptime because of this field
|
|
// :2:12: note: use '*const fn () void' for a function pointer type
|
|
// :32:19: note: called from here
|