On 2011-07-02 19:42, Adam D. Ruppe wrote: > Jonathan M Davis wrote: > > The range has to be mutable. > > Is there any easy way around this, aside from casting away > the outer immutable? > > It's a very annoying limitation that doesn't really have to be > there - I believe the language itself would let you pass immutable > int[] to a regular function that expects immutable(int)[], since the > outermost reference is passed by value anyway.
The problem is how the type is determined with templates. The _exact_ type is used. So, instead of generating a function which takes immutable(int)[], the compiler generates a function which takes an immutable int[], because you passed it an immutable int[], not an immutable(int)[]. And it doesn't work to iterate over an immutable int[], so the function fails to compile. It's the same reason why you can't use static arrays with range-based functions unless you slice them. They don't work when the type is a static array, since you can't pop elements off of a static array. If the compiler were smart enough to use immutable(int)[] instead of immutable int[], and to use a dynamic array instead of a static one when choosing the type for the template, then the problem would be solved. But it isn't that smart. There are at least a couple of enhancement requests which relate to the issue, and it may be resolved at some point, but as long as the compiler uses the _exact_ type given for the template even when a more relaxed type would work, you're not going to be able to pass immutable arrays or static arrays to templated functions. - Jonathan M Davis
