Smart pointers instead of GC?
Matthias Einwag
matthias.einwag at googlemail.com
Wed Feb 5 00:09:19 PST 2014
>> Again, what happens with:
>>
>> T identity(T t) { return t; }
>>
>> ? I.e. the transference of the argument pointer type to the
>> return type?
>
> As far as I see, in Rust the pointer type is lost on the return
> value as well, if your function takes a reference (borrowed
> pointer). But you can do:
>
> fn identity(x: &T) -> T {
> return *T;
> }
>
> fn main() {
> let a = ~T;
> let r = ~identity(a);
> }
>
> That is: pass by reference and return a value type. If you then
> pass an owned pointer ~T, you can directly construct the
> returned value in heap allocated memory ~identity(a) and assign
> the resulting owned pointer. The compiler is smart enough to
> not do unnecessary copies.
This would definetly make r an unbecessary heap copy.
Return *x would also only work for POD values, because for all
others it would cause a (not allowed) move ibstead of a copy. For
a copy you would have to return x.clone().
You can return by reference in rust, but you have to annotate
everything with proper lifetimes.
Possibly incorrect example:
fn identity<'a>(x: &'a T) -> &'a {
x
}
I dont think the comparisons to rust make too much sense because
the whole system works completely different. For D the question
mainly is how to manage classes (and also interfaces to them)
with GC or ARC. Rust at the moment can't manage any
interfaces/traits with theit smart pointers.
More information about the Digitalmars-d
mailing list