4 ms·
Anyone here uses D? I recall it was kind of popular a few years back, and its features look very similar to Rust. Thoughts?
by happywolf 11y ago
Anyone here uses D? I recall it was kind of popular a few years back, and its features look very similar to Rust. Thoughts?
- Ace17 11y agoI use D for almost everything I do. A lot of the cool stuff being integrated in modern C++ has been present in D for ages ('enum class', 'string_view', 'constexpr', 'auto', ...) ... D has a real module system (=it compiles fast) and an extremely powerfull template system. It's one of the rare languages allowing introspection (e.g you can statically loop over a struct members) without bringing in "dynamic" stuff (e.g add members at runtime, etc.).
- aidenn0 11y agoAndrei Alexandrescu points out one major issue being that D went with a GC first approach, and even though the GC is optional, the library support isn't as good. For at least some problems people will not want to use a GC, and D's story there isn't as good as Rust's.
- Ace17 11y agoTo me, garbage collection is a hack, trying to workaround the fact that we don't know how to deal with ownership. This is something Rust and C++ get right (unique_ptr, the whole Rust ownership system), and I would definitely prefer D being based on an ownership system, rather than a rogue thread randomly scanning your memory for things that look like addresses. However, the garbage collection in D turns out not to be an issue in practice. In D, you do have destructors and deterministic deallocation when you want to (in one word: RAII).
- jcranmer 11y agoThe problem with memory management is that, in any large system, you invariably have a situation where multiple need to share ownership of a resource (think std::shared_ptr or Rusts' Rc<T>). And in large systems, such ownership graphs may form cycles. Now you can build systems that detect cycles who lack ownership outside of the cycle and break them. We call those systems garbage collectors.
- Aeolos 11y agoThe thing is... std::shared_ptr is slower than a modern GC. It also doesn't protect you from memory fragmentation. GC is indeed a hack: it approximates an infinite memory system on systems that have finite memory. Reference counting is a worse hack: it approximates a GC on languages/systems that can't afford a real GC.
- gpderetta 11y agoGC is not an hack. Short of designing your language around ownership (like rust, Cyclone), it is the only way to guarantee memory safety, which is a fundamental property for a programming language. The automatic memory management part is almost just a nice bonus on top of that. And I say this as a C++ programmer that has written very little code on GC'd languages.
- theseoafs 11y ago> To me, garbage collection is a hack, trying to workaround the fact that we don't know how to deal with ownership. Eh. Often the whole "ownership" thing is irrelevant. For a lot of high-level programs, trying to express things in a Rust-ish "ownership" style with pointers adds a lot of syntactic and semantic overhead.
- danieldk 11y agoOften the whole "ownership" thing is irrelevant. I don't see how it can ever be irrelevant, unless you use immutable data structures everywhere. Does your class/struct provide getters? Ownership! You have to think about what you return. When you are not careful, the calling code can break invariants of the data structure. Consider the following Go code: type Foo struct { sortedSlice []Bar // .... } func (f *Foo) sorted() []Bar { return f.sortSlice // Outside world can break invariants. } Now the outside world has access to internal state and can break invariants (make sortedSlice unsorted). You have to decide to: make a copy (and waste time) or document this fact (and put the burden on the user of Foo). Suddenly, knowing from the signature what the ownership is and having the compiler fail on violations, looks pretty useful! Tony Hoare once called the null pointer his one billion dollar mistake. I think the lack of ownership semantics (or immutability) is an even bigger mistake. Some of the most difficult and aggravating bugs that I encountered (even in relatively simple web services) were caused by (a lack of understanding of) ownership. Throw in some concurrency and you have bugs that occur frequently, but are terribly hard to reproduce consistently.
- petke 11y agoGarbage collection only works well for memory though. Other resources like file handles, mutexes, database connections, etc. You don't want to leave those hanging. You want them to be released at once a scope is exited.
- im2w1l 11y ago>a rogue thread randomly scanning your memory for things that look like addresses. How does that avoid race conditions? What happens if I quickly move my pointer around? i.e. b=a;a=null;c=b;b=null;d=c... etc
- Aeolos 11y agoIt still works correctly. Try it.
- fsloth 11y ago"To me, garbage collection is a hack," Garbage collection trades the problems gc brings with less program complexity and programmer time. It's a degree of freedom that one can choose (if one can choose the language one uses). "Reserve memory for this entity when I declare it and release it when I don't seem to be using anymore" is huge leap in getting computers to assist us in our tasks. But sure, there are lot of problems domains where this is not a good thing.
- charlieflowers 11y agoD is awesome. Very expressive, very well designed. But, to use the D standard library, you have to use (the otherwise optional) garbage collection. So, if your domain dictates no garbage collection, D is not really an option (technically, you could use D without the standard lib, but that's not very pragmatic in most cases).
- WalterBright 11y agoI use D every day.
- fsloth 11y agoAre there any niches of C++ where one would be still better of using C++ than D?
- WalterBright 11y agoProbably the largest barrier to using D is needing to hook up with existing C++ code. D is already doing tolerably well with that. For example, the front end of the D compiler is written in D and hooks up to the 3 backends which are in C++. We're working at making it much better. Recently added, for example, is the ability to catch exceptions thrown by C++ code.
- fsloth 11y agoThanks! And how is deployment to the platforms that run on common mobile handsets?
- WalterBright 11y agoI don't know the state of that at the moment. You'll have to ask on the D forums.
- lultimouomo 11y agoLast time I looked at support for ARM it was quite lacking (no exception support, the compiler needing patches to compile, test suite not passing). There were people working on this, but unfortunately it didn't seem a top priority for the community. I'm not sure why this is, as D would seem a perfect fit for writing stuff for the Raspberry Pi and similar; the other usual suspects have focused on ARM support almost from day 1. Lending a hand there it's been near the top of the list of "things I'd like to do if I had some spare time" for a long time, but I haven't come around that yet. It looks like a fun project, and not one you come across often. Want to write "Helped port a major programming language to ARM" on your resumé? Hurry up!
- giancarlostoro 11y agoI come from Java / C# and I love D. I've tried Go, but I just can't give up on classes and other things. D has an amazing community, the documentation is good enough, and sometimes I just "try" code and it just works. To come to D you need a bit of an open mind since a few things are slightly different, but you will have fun, I know I do.