Smart pointers instead of GC?

Frank Bauer y at z.com
Tue Feb 4 17:34:58 PST 2014


On Wednesday, 5 February 2014 at 00:50:55 UTC, Walter Bright 
wrote:
> 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.

That way the caller is responsible for the relationship between 
argument pointer type and result pointer type.

With a single function, you can freely mix all combinations of 
argument and return types. E.g. pass an owned pointer and 
construct a Gc<T> from the returned result.


More information about the Digitalmars-d mailing list