4,901 karma · joined Sep 13, 2020 (6y ago) · 1,641 submissions
comment 5y ago · link · parent
This "foo" still suffers from the same problem as "Option::map" does: it is hard-coded for functions of fixed arity (2 in this case, 1 in Option::map's case).
Needing to write this function doesn't shorten the code. The SML-inspired solution allows one to write an "Option::map" which accepts functions of all possible arities (since they are all 1).
But yes, if there are a lot of places where I want to call "Option::map" for an "f" which takes 2 arguments, that could work. Although, personally, I think I'd still opt for an explicit lambda, in order to communicate intent more directly (avoid one level of indirection).
comment 5y ago · link · parent
That's great news.
> that could solve your issue if `mem::disciminant<Enum::Variant>` returned what you wanted.
I think you misunderstood, or I explained it poorly.
Given an:
enum Enum {
A(int),
B,
}
I would like mem::Discriminant<Enum> be the same type as if there was such a definition in code (illegal in Rust, because Rust doesn't have specializations): enum mem::Discriminant<Enum> {
A,
B,
}comment 5y ago · link · parent
* the function's argument is a tuple (this is the more common way, not only in SML, but also how it's done most commonly in maths);
* currying.
I'm not a fan of currying, because it singles out the first argument over the others. However, the first approach (tuples) is very ergonomic, because now you can pass the return value of one function as an argument to a "multi-argument" function. One application of that is function composition. But another is a simple map. I've lost count of how many times in Rust I had an "o: Option<(A, B)>" and a "f: fn(A, B) -> C" and couldn't call "o.map(f)"; instead the programmer is forced to write more noisy code with lambdas. And lambdas sometimes don't play well with the borrow checker for mysterious reasons (even those that don't capture anything).
Also, I think it would be nice if the compiler generated named discriminants for every enum. Currently, "std::mem::discriminant" returns an opaque "std::mem::Discriminant<T>" (which doesn't have a name). In order to compare discriminants I need to create a full-blown enum value and extract the discriminant from it later. I've worked around this problem by using the "strum" crate, which has a macro to produce another (plain) enum, but having the discriminant type be named "std::mem::Discriminant<T>" (to signal the connection to the original enum) and have named variants at the same time would be the best of both worlds.
EDIT: If I knew more about possible breakage caused by my first suggestion, I could probably start working on an RFC. However, I feel I don't have enough knowledge about Rust to anticipate that.
For my second suggestion I think I'm going to try to propose that. I'm reading the docs on the process now.
comment 5y ago · link · parent
This is going to be difficult: <https://arstechnica.com/information-technology/2014/07/the-n...>
comment 5y ago · link · parent
There are companies like Sun Microsystems, and then there are companies like Oracle.
There are companies like Gandi, and then there are companies like GoDaddy.
There are companies like Fastmail, and then there are companies like Google.
I prefer to look up to the first group.
comment 5y ago · link · parent
> Mongolian culture is famous, along with Tibetans, for "sky burial," which leaves the body of the deceased on a high unprotected place to be exposed to the elements and devoured by wildlife. It's part of a Vajrayana Buddhist outlook about the needlessness of "respecting" the body after death.
Personally, I'd prefer if this was what would happen to my body than any Western ritual. Alas, in the West we have laws which, in practice, impose religious precepts long after the states are secular on paper.
[1]: https://www.bustle.com/articles/97030-5-interesting-death-an...
comment 5y ago · link · parent
[1]: <https://www.eclipse.org/mat/about/screenshots.php>
EDIT: A bit more googling and I found <https://memprofiler.com/> for .NET which seems to have a similar feature, called "Instance Graph".
comment 5y ago · link · parent
comment 5y ago · link · parent
This is done at compile-time by the compiler. We're talking about division by a constant, not by a variable. So it's always faster.
comment 5y ago · link · parent
comment 5y ago · link · parent
1. We have more ozone holes now.
2. We weren't constantly in direct contact with sunlight. If you move past very early stages, we had shelter in forests, caves, tents, igloos.
3. When we were in direct contact with sunlight, we had a darker skin, which is more resilient to that.
Correct me if I'm wrong.
comment 5y ago · link · parent
comment 5y ago · link · parent
1. Functions which convert a number to a string, where the target string is a byte buffer passed into the function as a pointer argument. This allows C developers to pass pointers to statically-sized arrays of bytes, which leads to buffer overflows. (Inb4 someone shows up with "but that's the programmer's fault for not using an array that's big enough".) Example: strcpy.
2. Functions which return pointers to static storage, which makes them thread-unsafe, and even in single-threaded cases is error-prone, when you call one such function multiple times in succession (aliasing). Example: gmtime.
Both cases are very prominent in the standard library.
If instead functions allocated memory dynamically, then they would avoid buffer overflow, because size would be handled exclusively within those functions, allocating as much memory as is needed, no more, no less; instead of forcing the programmer to worry about the size at every call-site. They could also be easily made thread-safe and less error-prone (eliminating aliasing), provided that the allocator is thread-safe.
Another issue with the first case is that it leads to uninitialised variables. Often a pattern of "bool NumToString(int n, char* dst, int size);" is employed instead of "Optional<std::string> NumToString(int n);". This invites the possibility of using "dst" without checking the return value, which says if the conversion succeeded. In this case dst is most likely uninitialised, and definitely incorrect. An "Optional<std::string>" solves both issues.
comment 5y ago · link · parent
Also, concatenating strings for any other purpose than printing them is a rarely done thing in my experience, no matter the language. So I don't see why we should optimize the ergonomics of something like that.
comment 5y ago · link · parent
I prefer the "meditative" approach, because it will more often produce something readable. It looks unproductive, because there is a small number of lines produced in a given amount of time, but that's actually good. With the approach where the programmer just sits down and bangs away at the keyboard straight away, producing lines of code at uniform velocity, you end up with loads of code doing nothing, "dowork", calling "doworkimpl", stack traces 50 stack frames long where no stack frame seems to be doing any useful work. The "meditative" approach in extreme cases can produce indecipherable code with y combinators and whatnot, if your coworker is a real monk, but generally brevity seems to be a nice benchmark. All those great Haskell abstractions don't actually produce short code, it's the basic Haskell that does it. It just takes a while to arrive at such a solution, be it C or Haskell. And it's a win long-term, when you need to get back to this code to debug it. And you are going to need to get back to debug it.
See e.g. The Evolution of a Haskell Programmer[1]. It's not the great abstractions that produce the shortest code. But it is the shortest code that is the clearest.
[1]: <http://pages.cpsc.ucalgary.ca/~robin/class/449/Evolution.htm>
comment 5y ago · link · parent