140 points · by magnio · 4y ago · 52 comments
Next time, trigger warning please! I still have PTSD from that time I tried to text flow around an image correctly back in ‘08 on deadline. I just try to think of God and country instead…
It might interest to some to know that GCC has a macro called __COUNTER__ (apparently also implemented by clang and MS), which auto-increments its value. You can then create something like a gensym to avoid conflict.
Here's an implementation of the "defer" keyword in C++:
template <typename F>
struct privDefer {
F f;
privDefer(F f) : f(f) {}
~privDefer() { f(); }
};
template <typename F>
privDefer<F> defer_func(F f) {
return privDefer<F>(f);
}
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = defer_func([&](){code;})
It was written by gingerBill, author of the Odin programming language.Sure there are other "newer" ways but they invariably involve so much boilerplate that you might as well just write all the code by hand.
- Varargs are, generally speaking, a pain and a wart, especially in statically typed languages
- Printing stuff is horribly unergonomic without varargs (C++'s iostream << is arguably a get-out here, but is widely hated),
- There are no other compelling 'mainstream' uses for varargs afaik
By having print as a macro, Rust can swerve this particular tarpit. Not suggesting that it's the reason for macroising print, but it's a welcome bonus nevertheless.
See e.g.: https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c...
These functions are especially tricky because we want to have a variable number of arguments with arbitrary types. We want to be able to print two bytes, a string, an array, and six different arbitrary data structures we decided were important.
There are lots more applications for function(a, b, c, d, ...) where a, b, c and so on are all the same type. It might be nice to define min() and max() over these parameters for example. But cases like print-format where it's sane to choose unrelated types are rarer. Languages where I can write min("Cheese", 4, tmpfile, -Inf) have lots of other problems that outweigh the potential usefulness of such constructions.
I expect that if somebody can write a sound but ergonomic varargs story for safe Rust it'll get introduced, even if they can't solve print-format it's still useful as I showed above.
Varargs here would really just be a minor ergonomics improvement.
Heterogeneous varargs would be more useful, but would require something like variadic generics. (which have been proposed before)
[1] I say this as a big fan of clojure
Also, if you really, really want types, as in Haskell-like types... there's Coalton[2], a language that's embedded in Common Lisp (CL is that powerful it allows things like this) with a ML type system and more "modern" idioms for people used to functional programming.
Let the panic set in, don't try to change the subject. Imagine as the seconds slip by that you struggle to reflect on all the parts of your life. 5 minutes can seem like a long time, but when it is your last few seconds they vanish quickly.
Really be in that moment. Take it seriously. Embrace the terror.
When the timer goes off none of the problems in your life will seem one-tenth as serious as before.
Disclaimer: I’m not in debt, pain, legal/criminal trouble, not relatively poor and not suicidal.
Soon you'll be dead, and everyone you love, everyone you know. And everyone who knows them, and everyone who knows them, and so on on down the line. And not just dead. Forgotten. Completely.
Does that change your priorities in life at all?
I take two things away from this exercise.
1. Try to be happy in the moment.
2. Try to build things that will outlast you. Nobody remembers the names of the architects of the Roman aqueducts, but people in Italy still benefit from their work today.
2 is most done reliably by having children. Nevertheless, Vitruvius?
Enjoy your irrelevance! :-)
There is also M-x pp-macroexpand-last-sexp that comes with Emacs. And the macroexpand function itself of course.
I recommend both, and the punch... line made me LOL IRL.
I am sure you're trying to make friends in the rust community with the "on error resume next" thing!