function values as comptime params

This commit is contained in:
2026-07-18 01:35:39 +02:00
parent 9f433af724
commit 85693e57e1
7 changed files with 254 additions and 12 deletions
+43
View File
@@ -9,6 +9,10 @@ Point :: struct {
y i32
}
Callback_Config :: struct {
call @func(value i32) i32
}
Box :: union(enum) {
point Point
empty void
@@ -78,6 +82,26 @@ increment func(value i32) i32 {
return value + 1
}
double func(value i32) i32 {
return value * 2
}
decrement_c c_func(value i32) i32 {
return value - 1
}
apply_comptime func($callback @func(value i32) i32, value i32) i32 {
return callback(value)
}
apply_comptime_c func($callback *c_func(value i32) i32, value i32) i32 {
return callback(value)
}
apply_comptime_config func($config Callback_Config, value i32) i32 {
return config.call(value)
}
call_native func(callback @func(value i32) i32, value i32) i32 {
return callback(value)
}
@@ -180,6 +204,10 @@ main func() i32 {
recovered i32 :: $recover()
storage i32 :: $storage_mutation()
called i32 :: $call_native(increment, 11)
comptime_callback i32 :: $apply_comptime(increment, 12)
comptime_literal i32 :: $apply_comptime(func(value i32) i32 { return value + 2 }, 12)
comptime_c_callback i32 :: $apply_comptime_c(decrement_c, 15)
comptime_config i32 :: $apply_comptime_config(Callback_Config {call = increment}, 15)
fallible_ok i32 :: $call_fallible(may_fail, false) catch 0
fallible_err i32 :: $call_fallible(may_fail, true) catch |e| {
result i32 :: match e {
@@ -239,5 +267,20 @@ main func() i32 {
if ERRDEFER != 42 {
return 17
}
if comptime_callback != 13 or comptime_literal != 14 or
comptime_c_callback != 14 or comptime_config != 16 {
return 18
}
if apply_comptime(increment, 20) != 21 or apply_comptime(increment, 21) != 22 or
apply_comptime(double, 20) != 40 {
return 19
}
if apply_comptime(func(value i32) i32 { return value + 3 }, 20) != 23 {
return 20
}
if apply_comptime_c(decrement_c, 20) != 19 or
apply_comptime_config(Callback_Config {call = increment}, 20) != 21 {
return 21
}
return 0
}