DEV Community

Discussion on: How I want my command inputs, but nobody agrees

Collapse
 
legolord208 profile image
jD91mZM2

I like your approach! It's sad however that it requires a map :(

Collapse
 
wefhy profile image
wefhy • Edited

In fact it does not. You can use it even in C with structures like:
gist.github.com/wefhy/4fa3039bac8a...
And additionally you can automatically generate help message from this.
But I also love Rust for things like this ;)

Collapse
 
secretlyjaron profile image
Not Jaron

In C/C++ you could do something like this with XMacros so the mapping gets taken care of at compile time. Might be able to come up with a solution using this: internals.rust-lang.org/t/x-macro-... - Kind of a pain to set up, though!

Collapse
 
geordiepowers profile image
Geordie Powers

It's not ideal, but I'm not too concerned. In my benchmarks versus the switch style, it's about half as fast to process a command. Startup time is of course greatly increased, but since the system allows for new commands to be later added during runtime, program startup doesn't need to happen often.

I can't speak for every situation, but my Discord bots aren't super performance-critical, so I find the maintainability and expressive freedom offered by this solution to be a much bigger win.