How I learned to stop worrying and love macros
zdimension.fr
zdimension.fr
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.
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)
See e.g.: https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c...
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.
[1] I say this as a big fan of clojure
There is also M-x pp-macroexpand-last-sexp that comes with Emacs. And the macroexpand function itself of course.
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.
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?
There's a moral argument I could make that suggests even with high costs lots of people would get it anyway out of a sense of civilizational fairness, or a history-of-technology argument I could make about tech getting cheaper over time, but I think the argument I like best and that works on its own is just pointing out that there's a huge economic incentive to government and society more broadly to provide such technology to everyone even for free (from their individual perspective at time of treatment).
On the production side is the incentive of having healthy citizens who don't have to stop working on account of age. There's something like 16% of the population just in the US who are older than 65, something like 20% of them are still working, and some of those not working would be more than happy to go back to working but can't because of age-related issues (the final one being death). Adding some of them back to the economy would be a nice boost worth paying a lot for.
From the spending side, currently governments subsidize medical treatment for lots of people, sometimes even for everyone and even for brand new technologies like mRNA vaccines. There's a huge network of so-called insurance providers which also play into this; some things are fully covered by a plan while others are subject to more complicated rules. The dynamics of how money actually flows around and who really pays for what are complicated but society as a whole is currently paying a lot of money for the benefit of individuals. So we can expect longevity treatments to be subsidized and affordable for some people to some extent because that's the case with every other medical treatment. We can expect it to be a large number of people, trending towards everyone, and heavily subsidized if not otherwise affordable, trending towards free (from the individual perspective), because of the nature of the 'disease' (age affects everyone, like a global pandemic) and the consequences of treating it or not. We already know the consequences of not treating aging, and a lot about their expenses. So much is spent just on end-of-life care (for US Medicare alone, 13%-25% of its budget is spent during recipients' last year of life) let alone all the age-related issues leading up to that. The government or society more broadly spending money on individuals now for otherwise unaffordable rejuvenation tech can make a lot of sense when the alternative is spending a lot more money on them not too much later for unaffordable end-of-life care.
Enjoy your irrelevance! :-)
Disclaimer: I’m not in debt, pain, legal/criminal trouble, not relatively poor and not suicidal.
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!