What should array() return for const/immutable ElementTypes?

David Nadlinger see at klickverbot.at
Sat May 5 11:43:17 PDT 2012


On Saturday, 5 May 2012 at 17:48:34 UTC, bearophile wrote:
> Ideally the most useful result is a mutable one that is 
> implicitly castable to immutable if the mapping function is 
> pure. But I don't know if this is always possible.

Implicit conversion to immutable would only work if array() is 
strongly pure, but this would require the passed in range type to 
have no non-immutable indirections (besides needing a pure 
implementation of array(), which is currently not the case due to 
appender, but this can be fixed). OTOH, I think it _could_ work 
for ranges operating on originally immutable data, but it seems 
like it doesn't typecheck in today's DMD/Phobos:

———
auto array(Range)(Range r)
if (isIterable!Range && !isNarrowString!Range)
{
   alias Unqual!(ForeachType!Range) E;
   E[] result;
   foreach (e; r) result ~= e;
   return result;
}

void main() {
   immutable x = [1, 2, 3];
   immutable y = array(filter!"a == 3"(x));
}
———

David


More information about the Digitalmars-d mailing list