On Sunday, 19 February 2017 at 12:31:51 UTC, ag0aep6g wrote:
On 02/19/2017 12:51 PM, timmyjose wrote:
a). So the GC is part of the runtime even if we specify @nogc
Yup. @nogc is per function, not per program. Other functions
are allowed to use the GC.
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?
You can trigger a collection manually with GC.collect [1].
Otherwise, the GC can do a collection when you make a
GC-managed allocation. If you don't make GC allocations, e.g.
because you're in @nogc code and the compiler doesn't allow you
to, then no GC collections will happen.
c). Does Rust have analogues of "new" and "delete", or does it
use
something like smart pointers by default?
D, not Rust, right?
Yes, indeed!
D uses `new` for GC allocations. `new` returns a raw pointer or
a dynamic array (pointer bundled with length for bounds
checking).
There is `delete`, but it's shunned/unfashionable. Maybe it's
going to be deprecated, I don't know. You're supposed to let
the GC manage deletion, or use `destroy` [2] and GC.free [3] if
you have to do it manually.
Of course, you can also call C functions like `malloc` and
`free` and do manual memory management.
I actually tried out some of the sample programs given on this in
"Learning D", and it was quite smooth indeed. As ketmar mentioned
in the other thread, maybe I could use this as a backup strategy
till I get comfortable with idiomatic D.
A few things I already like so far about D (just on chapter 2 of
the book!):
1). T* x, y applying the pointer type to both variables (this has
been a bane for me with C in the past).
2). The cast(T) syntax.
3). The module system appears pretty logical to me so far.
4). The creation of dynamic arrays is quite smooth and intuitive
for me, and much easier than in C or C++.
5). I love array slices!
6). Properties!
7). The array initialisation syntax (especially for rectangular
arrays) - much more logical than in C++/Java.
8). The use of %s for string interpolation (just like Common
Lisps' ~a). Very convenient.
Things I don't like so much:
1). The std.range: iota function(?) is pretty nice, but the
naming seems a bit bizarre, but quite nice to use.
2). The automatic conversion rules are nice for avoiding verbose
code, but it looks like it might bite one just like in C++.
3). Not so much a fan of "auto", but it does have its uses, of
course.
4). I'm still a bit confused by order of dimensions in
rectangular arrays:
Suppose I have a simple 2 x 3 array like so:
import std.stdio;
import std.range: iota;
void main() {
// a 2 x 3 array
int [3][2] arr;
foreach (i; iota(0, 2)) {
foreach(j; iota(0, 3)) {
arr[i][j] = i+j;
}
}
writefln("second element in first row = %s", arr[0][1]);
writefln("third element in second row = %s", arr[1][2]);
writeln(arr);
}
My confusion is this - the declaration of the array is arr
[last-dimension]...[first-dimension], but the usage is
arr[first-dimension]...[last-dimension]. Am I missing something
here?
Regarding smart pointers, I'm not up to speed. There's
std.typecons.Unique [4], but I don't know how it compares to
other languages.
[1] https://dlang.org/phobos/core_memory.html#.GC.collect
[2] https://dlang.org/phobos/object.html#.destroy
[3] https://dlang.org/phobos/core_memory.html#.GC.free