7 ms·
D seems like a more useful Rust, or is that completely wrong?
by afghanPower 9y ago
D seems like a more useful Rust, or is that completely wrong?
- joobus 9y agoD is garbage-collected, but can be opted out of. Rust has no garbage collector.
- yorwba 9y agoRust has an opt-in garbage collector as a library (https://docs.rs/gc/ https://docs.rs/gc/). I don't know how good it is, though.
- Rusky 9y agoBy that measure, so do C and C++.
- tcbawo 9y agoYou could implement GC in C++ using RAII (although smart pointers get you a long way). AFAIK, automatic GC is not possible with C.
- antientropic 9y agoIt absolutely is possible, via conservative GC: https://en.wikipedia.org/wiki/Boehm_garbage_collector https://en.wikipedia.org/wiki/Boehm_garbage_collector
- timlyo 9y agoWhat's the use case for a garbage collector in rust?
- yorwba 9y agoIf you have to share some data across your program and can't determine the lifetime at compilation time, you can't simply rely on the Rust compiler for memory management. The next option is to use reference counting by wrapping your data in Rc/Arc (depending on whether you need atomicity or not). But that can still leak memory if you have cyclic data structures and can't break the cycle with Weak pointers. At this point, what you need is a garbage collector.
- nenreme 9y agoThey are very different languages. Rust was designed to be GC-free and memory safe. D, on the other hand, has GC and is memory unsafe by default.
- pjmlp 9y agoD being @system by default has already been discussed as a design error, but making @safe be the default would be a breaking change. So it is up to the community if they want to accept such change. I should note that they still need some help cleaning the standard library and compiler corner cases in regard to @safe.
- wtetzner 9y agoCould there be a compiler flag to flip the default to @safe,so it doesn't have to be added to each file?
- pjmlp 9y agoIt could eventually be an option I guess, but it would require anyway to recompile all code. As always in such cases, to validate binary libraries, they need some kind of metadata to indicate they are safe libraries (aka they only use of @safe or @thrusted code). .NET does this with MSIL metadata, Modula-3 does it directly on the module definition section, for example.
- destructionator 9y agoWell, if you actually use the GC and array range checks (also enabled by default), it gives you memory safety in the vast majority of cases. Though, indeed, the language does let you break that by default, it isn't something you are likely to do accidentally. GC + range checks provide memory safety to most programs without the kind of extra work you need in Rust. This is a big reason why they are so common in industry.
- issaria 9y agoCall me wrong if I misunderstood, but isn't reference counting also kind of GC?
- deleted 9y ago[deleted]
- Dzugaru 9y agoIn my experience, D is like more powerful and feature-packed C#. It doesnt impose any limitations on you and your coding style/paradigm and strives to be "the one to rule them all" tool which has everything and can be used for everything from scripting to systems programming (when stdlib will be more @nogc friendly). And yes, templates are so much better in D than in C++ or in C# :)
- hello_there 9y ago> when stdlib will be more @nogc friendly How does @nogc work in D? Is it easy to keep track of what needs freeing and what does not or is it easy to mix up and get hard bugs? Also, what do these bugs look like? Is use-after-free possible or how is the failure mode i that case? Is it possible to call free on an object after it's been garbage collected?
- eco 9y ago@nogc just causes the compiler to error out when a GC allocation occurs in the region marked @nogc and its call graph. You are then expected to manage memory yourself. You could rename it @c_or_cpp_style_memory_management_only. You can malloc/free (scope(exit) is useful here), use smart pointers, alloca, static arrays, use Andrei's allocators[1], or whatever else you'd use in C or C++. The answer to all of your questions is basically the same as they are in C and C++. D does have @safe though which prevents unsafe memory operations and Walter is in the process of ratcheting up the memory safety with DIP1000[2]. 1. https://dlang.org/phobos/std_experimental_allocator.html https://dlang.org/phobos/std_experimental_allocator.html 2. https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md
- kronos29296 9y agoIIRC Rust and D have different targets for use. Rust is for safe systems programming so that you don't get UB in C, while D is better C++. Also Rust new compared to D.
- liveoneggs 9y agoD is like Go if Go had actually been a "systems" programming language. Rust is difficult to compare to other stuff.
- eco 9y agoHow do you figure? D is C++ with lessons learned. I mean, it's right there in the name. I find very few similarities between Go and D. Also, D is much older than Go so if anything Go would be D if D decided to not be a "systems" programming language (but it's plainly obvious that isn't the case).
- acehreli 9y agoTo me, D's compile-time features like templates, code generation, etc. are some of its most important features. Lacking those and exceptions and many other useful bits, Go cannot compare to D. (I finally can claim experience on both languages after having coded Go for about a month.)
- int_19h 9y agoI think the point is that D targets the same set of scenarios as Go, but it's better at it for all the reasons that you've listed.
- _ak 9y agoGo is still a systems programming language, in the original sense of how Rob Pike explained it in the introductory presentation video about Go from 2009.
- liveoneggs 9y agoand I'm a professional basketball player in the way I explained it to my 8th grade class
- zakk 9y agoI like Rust, but I hate with a passion the borrow system. So I was looking for an unsafe-by-default Rust and I found D, thanks to the suggestion of a kind HN user. I must admit that I love it!
- iamNumber4 9y agoMore like D is a Better C.
- qznc 9y agoIt even has a -betterC flag.
- giancarlostoro 9y agohttps://dlang.org/dmd-linux.html#switch-betterC https://dlang.org/dmd-linux.html#switch-betterC
- eco 9y agoJust more pragmatic, I'd say. D isn't a "big agenda language" (to steal a line from Jonathan Blow). It's extremely multi-paradigm (some might argue to a fault). I think that's why you find people saying D is like C++ or D is like C# or D is like Go or D is like Rust. You get a little taste of everything using D. Want function programming and purity? Check. Want C style/low abstraction code? Check. Want extreme C++ metaprogramming? Check. Want C#'s LINQ? Check. Want an improved version of C++'s STL? Check. Want low cognitive load memory management through a GC? Check. Want highly tailored memory management? Check. Want high level object oriented abstractions? Check. Want memory safety? Check. Want systems programming? Check. Want rapid prototyping? Check. D, fundamentally, assumes the programmer knows what approach they should take and lets them do it. There are no "we know better" design decisions in the language. I think this might be because D is so community driven. With no real company backing D was left in the hands of enthusiasts coming from all sorts of different backgrounds to implement ideas they liked.
- geezerjay 9y ago> think that's why you find people saying D is like C++ or D is like C# or D is like Go or D is like Rust. You get a little taste of everything using D. That's a major problem, and not a feature. One of C++'s main drawbacks is its size and arcane features, to the point that the language is known for being impossible to master. If all D brings to the table is an agenda to pick off C++'s complexity and drive it up even further then I fail to see what problems that will solve while it creates many others.
- eco 9y agoBut it didn't drive up the complexity. It drastically simplified how a lot of features work. C++ is very difficult to master not because of the number of features in the language (it really isn't even all that featureful compared to other modern languages) but because of the thousands of unexpected details you have to know. Scott Meyers made a career out explaining them (and implored D not to make the same mistake of needing someone like him). That doesn't mean the overarching feature can't be implemented in a simple way that avoids the unintended complexity though. Anybody you ask with knowledge of both D and C++ would say that D's metaprogramming facilities are both drastically easier and more powerful than what C++ offers, for instance. It's actually shocking how much you need to know to fully understand things like template/regular type deduction (which aren't the same), initialization, rvalue behavior, forwarding references (or is it universal references...they came up with the feature before they gave it a name), what is constexper-able, reference collapsing, etc. These things are all straightforward in D because they were either designed without the edge cases and legacy behavior or left out entirely because the problem was tackled in a different, more simple way at a fundamental level.
- int_19h 9y agoI think D is more evolutionary than Rust. Rust tries to radically change the way programmers reason about their code. D is much more conventional in that regard, but accumulates all the power features from other languages on top of a "C++ fast" core and with a syntax that's familiar to someone coming from C++ (or Java and C#, for that matter).
- Ar-Curunir 9y agoFrom what I've read (and I may be off), but to me it does not seem that D provides many of the features that make Rust useful: algebraic data types, typeclasses, memory safety without a managed runtime.