Dynamic Array Garbage collection

bearophile bearophileHUGS at lycos.com
Thu Mar 12 09:58:19 PDT 2009


Jarrett Billingsley:
> Wrong types.

Right, I am sorry, thank you for spotting it.
There's the moderate need for a different kind of cast, for collections (arrays).
At the moment you can do:

import d.func: map, putr, array, xcast;
import std.conv: toInt;
import d.templates: ArrayType1;

void some_function(int[] x) {
    putr(typeid(typeof(x)), " ", x);
}

void main() {
    short[] x = [cast(short)10_000, 5_000, 100, 6_000, 50, 950];

    some_function(map((ArrayType1!(typeof(x)) s){return cast(int)s;}, x[0 .. 4]));

    // or less DRY:
    some_function(map((short s){return cast(int)s;}, x[0 .. 4]));

    // better:
    some_function(array(xcast!(int)(x[0 .. 4])));
}

The last version with array and xcast may be acceptable. The advantage of using it is that the ranges 0..4 can be variables.
Built-in types aren't constructors-functions, so you can't do something like this:

some_function(map(&int, x[0 .. 4]));

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list