77 points · by dumindunuwan · 10y ago · 70 comments
Seems to go very much into details about syntax bike shedding but not saying much about the more "global" design differences.
Combine GC and non-GC in a single application. This would be great for game programming for example. Handle audio and video in a soft-real-time non-GC thread. Handle other stuff like game logic in a GC thread, such that it is easier to write. The hard part is that GC usually requires stop-the-world. You can currently do this [0] in D, but it is error prone. Rust tackles this problem from the "other side": First make it safe, then possible.
[0] https://p0nce.github.io/d-idioms/#The-impossible-real-time-t...
Rust is actively working on this, in fact.
The idea is to eventually have a Gc<T>-like type, like Rc<T>, which can be used on specific things. It will interact fine with the regular heap, so `Vec<Gc<Foo>>` where `Foo` contains a `Box<Gc<T>>` will still work.
You can then selectively apply this Gc<T> to things which need it. For example if you have a large graphy-datastructure in your code and don't want to bother with weak pointers. Or if you are doing FFI with a language that does have GC.
Rust itself would not have the Gc<T> type in the stdlib, it would only contain a couple of traits and compiler intrinsics that are useful for a GC. (The idea is to be able to turn on LLVM stack maps and use it for GC rooting). So you can write a gc for your specific use case (even if that use case is just safe bindings to a different GC from a runtime you are interacting with!) and it should work. In fact, in the design so far it seems like multiple GC implementations will be able to exist in the same application, and perhaps even safely interact (you can control whether you want this to be possible using the type system; I'm not sure if there is ever a case where this would be needed)
This would of course, still be safe. It would not be possible to violate GC invariants using safe code, nor would it be possible for the GC to violate regular safety invariants.
I guess I'm mostly worried about Gc being used by a large number of libraries, to the extent that it might not matter if stdlib implements it or not, because by using some very popular library you'd end up pulling in this runtime dependency.
Part of me loves the flexibility this will bring to application design, but it could be a slippery slope.
IMO, Rust just not good enough for creating complex specialized data structures.
Even a trivial tree is hard to implement in Rust, and the implementation will be severely limited: https://github.com/SimonSapin/rust-forest
D surely has it’s problems as well (in short, I think the language itself is very good, but its runtime and libraries still need a lot of work), but at least it’s good for complex data structures.
I mean, you haven't actually pointed at a trivial tree implementation. The requirement that children be able to navigate to siblings is the issue here (complicating unique ownership by parents).
You could totally do a "trivial tree" as
struct Tree<T> {
payload: T,
children: Vec<Tree<T>>,
}
which your link even says. You would need a handle to the parent to surf around through siblings, is the limitation.It's not really clear (to me) that the rctree is really all that complicated; the Rc stuff is there to clarify ownership: for how long should allocated memory stick around. It might just read weirdly due to familiarity, in the same way that D might weird me out.
If you want you can always use raw pointers and dereference them in unsafe {} blocks, and even provide a safe API (return borrows with appropriate lifetimes) around this. You'll find plenty of common patterns in the standard library and on crates.io too.
For example, a vector that keeps some items + hash map that implements an index over that vector. Or some container that keeps some items + a linked list to track and evict the least recently used item from that container.
Rust is just too safe for that. That might be technically doable, but harder to implement, and harder to use.
This certainly applies to Rust’s built-in data structures as well. See this example, the problem is trivial to solve in most languages: https://www.reddit.com/r/rust/comments/3cz71e/holding_a_muta...
> See this example, the problem is trivial to solve in most
> languages
I disagree. That proposed code (again using references which provide guarantees that no other language provides) is failing because Rust statically prevents data races, which is not only non-trivial to solve in most languages, but actually impossible to solve in general in most languages. The way around this is to, again, regress to what other languages do and use raw pointers directly.I am frustrated with new languages. I agree that C++ is old and needs a more modern replacement but:
- Go is weird with no templates and no functional programming.
- Nim comes close to my dream language but has a GC and some very awkward features like globally imported namespaces.
If I had to start a new project I would probably pick Nim or C++.
Other languages do this too, for instance in Java you can override final with reflection. I lost my mind, both good and bad, when I discovered this.
By “efficient” I mean I want to split my list in two for O(1) complexity, like other programming languages do.
pub struct ListNode<T> {
data: T,
prev: Weak<RefCell<ListNode<T>>>,
next: Rc<RefCell<ListNode<T>>>,
}
The code linked above probably has bugs (for sure with singleton lists), but I hope you get the idea. It allows you to splice lists in and out in constant time.Also, the code doesn't use `unsafe`. Now, you would need to use `unsafe` somewhere to actually create a list, in order to initialize the fields (or you could change the type of `next` to be `Option<...>`). However, the use of `unsafe` would be limited to that method. This tries to get at the difference between (i) occasionally using `unsafe` and (ii) having no safety guarantees at all.
There are probably lots of other pain points that you can bring up, and I'm not trying to say this should be good enough, but I thought it might help explain what is possible.
Any prospect about that?
The other linked posts regarding meta-programming features of D are also great.
fn main() {
let x = true;
let x = false;
}
I was pretty disappointed when I found out about it.Some people don't like it, and if you don't you can use clippy to disallow it. From what I've seen, most Rust code does use shadowing.
Dun dun duuun
I used to write a lot of C++ and my idea of immutability always was that if I see a variable declaration at the top of a function and scroll down to where it's used (without reading the code in the middle) I can assume nothing happened to the variable.
Then I started to learn Haskell something like a year ago and found out that shadowing is not only allowed but used by people. Especially in Haskell (or OCaml for that matter) where you can just use x' for a slightly modified version of x. I don't know. Maybe I get used to it one day.
I don't know if it's idiomatic, but I find it keeps things cleaner by not having to define lots of different names for essentially the same variable.
Scope 1
let x = 1
let x = 2
=> error, rebinding variable in scope not allowed
Scope 2 let x = 1
shadow x = 2
=> works!
Scope 3 shadow x = 2
=> error: no variable x to shadow declared in scope
I wonder how well something like that would work in practice. fn main() {
let x = true;
{
let x = false;
}
}
Since you don't use let for assignment, it's a non-problem. And it can be useful when you're doing some computation where you want to keep intermediate variables (for debugging or clarity), and rather than think of the proper name for each, you just successively bind the same name. def fn(object):
let object = predicate(object) ? modify(object) : error();
// do something with the refined object
Or something along those lines. Can I write
let x = 1;
let x = x + 1;
?Shadowing within the same scope is admittedly unusual.
D: people don't.
I'm not an avid D user, but I have a guess as to why it is not so widely used.
To me, D is somewhat of a "I like C++s features and ergonomics and want a similar languages with all the features built in from the ground up" (+awesome metaprogramming). One of C++s drawbacks is that features have been bolted on in hacky ways due to backwards compatibility issues. D is like C++, but without this. It is a good goal, and from my limited experience with D I think they have achieved it well.
Thing is, such a language will largely be adopted by people who use C++. And the problem with these folks is that they already know C++. Once you are used to it it doesn't feel so bad. Despite its drawbacks, C++ has lots of tooling, a large ecosystem, and familiarity.
Rust is less affected by this for two reasons. One is that it gets a lot of production users from non-C++ shops who want to use a systems language to speed up their Python/etc tight loops. They would rather not worry about segfaults and stuff. The other is that for C++ users, Rust scratches a large itch. The learning curve for Rust is a lot worse than D, but for many the benefits might overcome this. You can't get used to segfaults the way you get used to template metaprogramming.
I would give D time. These issues just slow adoption, not stop it. That is fine.
Weka explicitly says "over 200K LoC"