Hello, folks! Newbie to D, have some questions!

timmyjose via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 20 07:00:05 PST 2017


On Sunday, 19 February 2017 at 12:45:49 UTC, ketmar wrote:
> timmyjose wrote:
>> a). So the GC is part of the runtime even if we specify @nogc
> yes. GC is basically just a set of functions and some 
> supporting data structures, it is compiled in druntime. @nogc 
> doesn't turn it off, if says that compiler must ensure that 
> *your* *code* doesn't allocate, at compile time. i.e. @nogc 
> code with GC allocations won't compile at all.
>
>
>> b). Do we manually trigger the GC (like Java's System.gc(), 
>> even though that's not guaranteed), or does it get triggered 
>> automatically when we invoke some operations on heap allocated 
>> data and/or when the data go out of scope?
> GC can be invoked *only* on allocation. as long as you don't 
> allocate GC data, GC will not be called. of course, things like 
> array/string concatenation (and closure creation) allocates, so 
> you'd better be careful with your code if you want to avoid GC 
> in some critical part. or you can call `GC.disable()` to 
> completely disable GC (and `GC.enable()` later, of course ;-).
>
>
>> c). Does Rust have analogues of "new" and "delete", or does it 
>> use something like smart pointers by default?
> `new`. no `delete`, tho, as it is not necessary with GC. 
> actually, there is `delete` thingy, but it is deprecated, and 
> you'd better not use it unless you are *really* know what 
> you're doing and why. i.e. don't prematurely optimize your 
> code, especially without good understanding of D's GC.
>
>
>> Fascinating reading about the various use cases that you and 
>> others have put D to. It does give me a lot more contextual 
>> understanding now. Thank you!
> you're welcome.
>
> as for me, i am using D exclusively for *all* my programming 
> tasks (including writing simple shell scripts ;-) for years. 
> and i don't want to go back to C/C++ or switch to some [new] 
> hyped language. i have 20+ years of programming expirience, and 
> i feel that D is the best language i ever used. don't get me 
> wrong, tho: it doesn't mean that D is the best language on the 
> planet. what i mean is that D has a best balance of features, 
> warts, libs and so on *for* *me*. easy C interop allows me to 
> use all the C libraries out there; C-like syntax allows me to 
> port C code (i did alot of C ports, including NanoVG, NanoSVG, 
> Tremor Vorbis decoder, Opus decoder, etc.); great 
> metaprogramming (for C-like language) allows me to skip writing 
> boilerplate code; and so on. ;-)
>
> also, dmd compiler is easily hackable. trying to even compile 
> gcc is a PITA, for example. and dmd+druntime+phobos takes ~1.5 
> minutes to build on my old i3.

Very interesting reading about your experiences! I hope that I'll 
soon be in a position to start churning out my own pet projects 
as well! :-) ... one thing I've observed is that so far (very 
very early of course) D appears to be a lot more intuitive than 
C++, or at least the way I find things intuitive, especially with 
regard to arrays and slices. The only thing I have to kind of 
unlearn is the "default immutability" that I picked up from Rust 
- this confused me a bit at first when I saw how a slice can be 
spawned off into a brand new array upon assigning data to it (in 
the book "Learning D", which I find very nice so far).

Just one question about the compilers though - I read on the Wiki 
that there are three main compiler distros - dmd, ldc, and gdc. I 
code primarily on a mac, and I have installed both dmd and ldc. A 
lot of the flags appears to be similar, and for my small 
programs, compilation and execution speed appeared to be almost 
identical. However, the book suggested using dmd for dev and 
probably ldc/gdc for releases. Is this really followed that much 
in practice, or should I prefer dmd?

One more thing I noticed when I looked into the executable file 
(using "nm -gU" on my mac) is that I found two interesting 
symbols - _main and _Dmain. On Rust, for instance, the main 
function got turned into _main, so I couldn't use a main in the C 
code that I was trying to interop with from my Rust code. In this 
case, does the same restriction apply (I am still way too far 
from dabbling in interop in D as yet! :-)). I mean, suppose I 
write some sample code in C, and I have a local main function to 
test the code out locally, will I have to comment that out when 
invoking that library from D, or can I keep that as is?



More information about the Digitalmars-d-learn mailing list