How to check if an array is a manifest constant?

simendsjo simen.endsjo at pandavre.com
Mon Apr 4 03:11:32 PDT 2011


> What use case do you have for wanting to know whether a variable is an enum or not?

The same reason I'd like to check if it's const/immutable; if an algorithm requires a
modifiable array.

void sortInPlace(int[] array) {
    array.sort;
}

void main() {
    int[] a = [3,2,1];
    sortInPlace(a);
    assert(a == [1,2,3]);

    auto i = [3,2,1].idup;
    //sortInPlace(i); // Fails at compile time

    enum e = [3,2,1];
    sortInPlace(e);
    assert(e == [1,2,3]); // fails at runtime
}

I can check the type for const or immutable with template constraits or static if's, but I
cannot check for enum.
I can use ref int[] as a parameter to get a compile time error, but that's misleading as I
often don't want to reassign the parameter.

Cannot the type of enum e = [1] be immutable(int[])? Or is that wrong?
Or am I missing something crucial because of CTFE or other stuff? I've just started looking
into D2, so theres a good possibility my reasoning is wrong.


More information about the Digitalmars-d-learn mailing list