Open TheDan64 opened 6 years ago
Working with heterogeneous collections is a pain in rust.. We could also provide a macro which simplifies the process for the user. (We may even want this earlier than 0.3.0
)
For example,
let values = mixed_value_array!(BasicValue, [
float_value,
int_value,
...
]);
If we do end up adding this macro, I'd strongly consider switching the current homogeneous function params from say, &[&BasicValue]
to &[BasicValueEnum]
since the former generally requires an extra heap allocation (unless you happen to have a fixed size of values on the stack, which probably isn't realistic) for the extra trait indirection which could be avoided.
Then, this macro could expand to something like let values: Vec<BasicValueEnum> = array.iter().map(|val| val.into()).collect()
. Might need associated type to derive BasicValueEnum
from BasicValue
, though.
The example function!
macro you showed above would require the power brought by proc_macro
, but it can be achieved similarly using a Lisp-like syntax (like clap.rs) with the macro_rules
system.
Here's an example of doing dynamic code generation using this syntax: primitives.rs (note: it's part of a legacy compiler I wrote last year, the code is ugly in some places). The macro itself is defined here.
Proc macros are probably fine; I don't expect to get to this issue before Macros 2.0 lands in stable(it seems pretty close: https://github.com/rust-lang/rust/issues/38356), but who knows.
It'd be pretty neat if you could do something like:
Which would return a function value or
Result<FunctionValue, Box<Error>>
with that definition. It might need to make some assumptions, like always using the global Context, though. Maybe you could optionally specify a context.