hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-19 18:53:06 +0200
committerhachem <im@hachem.wtf>2026-09-19 18:53:06 +0200
commitc6aab10e74d58fbcad723c5f074a0bb51e9f2283 (patch)
tree89d66cc61c99c4cc4b7824e3a3a2fabf3d5a674e /examples
parentcd2a86435e5e413630e0ab0caa48d900e49ea275 (diff)
feat: add functions with defines params and return
Diffstat (limited to 'examples')
-rw-r--r--examples/functions.sfinae55
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/functions.sfinae b/examples/functions.sfinae
new file mode 100644
index 0000000..d73261f
--- /dev/null
+++ b/examples/functions.sfinae
@@ -0,0 +1,55 @@
+// a function takes types and gives back a type. calling one is itself a type
+// it sees nothing but its parameters, and every argument must be a ::decltype so it carries a value
+
+::define<
+ ::name<'s', 'q', 'u', 'a', 'r', 'e'>,
+ ::parameters<::name<'x'>>,
+ ::sequence<
+ ::return<::decltype(::product<::value_of<::name<'x'>>, ::value_of<::name<'x'>>>)>::commit
+ >
+>::commit;
+
+::emit<
+ ::invoke_result_t<
+ ::name<'s', 'q', 'u', 'a', 'r', 'e'>,
+ ::arguments<::decltype(::difference<'F', 'A'>)>
+ >
+>::commit;
+
+// a function can return anything a type can be, which includes a tuple of characters
+::define<
+ ::name<'g', 'r', 'e', 'e', 't'>,
+ ::parameters<>,
+ ::sequence<
+ ::return<
+ ::tuple<
+ ::integral_constant<::char, ::difference<'h', '\0'>>,
+ ::integral_constant<::char, ::difference<'e', '\0'>>,
+ ::integral_constant<::char, ::difference<'y', '\0'>>
+ >
+ >::commit
+ >
+>::commit;
+
+::emit<::invoke_result_t<::name<'g', 'r', 'e', 'e', 't'>, ::arguments<>>>::commit;
+
+// a function that never returns gives back ::void, and emitting ::void prints nothing
+::define<::name<'n', 'o', 'p'>, ::parameters<>, ::sequence<>>::commit;
+::emit<::invoke_result_t<::name<'n', 'o', 'p'>, ::arguments<>>>::commit;
+
+// a return inside a branch still leaves the function
+::define<
+ ::name<'s', 'i', 'g', 'n'>,
+ ::parameters<::name<'n'>>,
+ ::sequence<
+ ::enable_if_t<
+ ::less<::value_of<::name<'n'>>, ::difference<'A', 'A'>>::value,
+ ::sequence<::return<::decltype(::difference<'A', 'B'>)>::commit>
+ >::commit,
+ ::return<::decltype(::difference<'B', 'A'>)>::commit
+ >
+>::commit;
+
+::emit<
+ ::invoke_result_t<::name<'s', 'i', 'g', 'n'>, ::arguments<::decltype(::difference<'A', 'Z'>)>>
+>::commit;