const debacle

Jarrod qwerty at ytre.wq
Mon Mar 24 20:57:49 PDT 2008


On Mon, 24 Mar 2008 13:35:14 +0000, Janice Caron wrote:

> On 24/03/2008, Jarrod <qwerty at ytre.wq> wrote:
>>  Maybe I'm tired, but how does sliceof() fix this? =/
> 
> It indicates to the compiler exactly what you want to do. Knowing this,
> the compiler is then able to syntax-check the function for errors, on
> the assumption that the only valid return values are slices of the
> specified variable. Attempting to return anything else becomes a
> compile-error. In effect, it allows the compiler to infer the explicit
> cast which Daniel919 wrote in explicitly. Remember, if the compiler
> cannot prove that it is safe, then the compiler must insist on the
> presence of an explicit cast. By telling the compiler exactly what you
> want to do, you allow the compiler to deduce that such a cast would be
> safe.
> 
> But returning a range would fix it better! :-) It's more powerful, and
> /way/ less confusing.

I would say it's less powerful. 
It only pertains to returning slices of arrays. What about returning 
elements from a struct or class ref, or subelements inside arrays? Const 
is transitive to my knowledge, so you couldn't do this normally. sliceof 
won't fix this either. The best you could do in the same vein as your 
solution is invent a keyword that takes an input's storage class and can 
use it for a return value:
storagetype(a) T[] (const T[] a, const T[] b);
But that would probably get problematic for returning nested things..

It's kind of a tough problem.



More information about the Digitalmars-d mailing list