Range question

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Feb 17 22:27:06 PST 2012


On Sat, Feb 18, 2012 at 08:02:15AM +0200, Mantis wrote:
> 18.02.2012 7:51, H. S. Teoh пишет:
> >On Sat, Feb 18, 2012 at 05:19:52AM +0200, Mantis wrote:
> >>18.02.2012 2:50, H. S. Teoh пишет:
> >>>...
> >>You cannot have ref local variable, so e is a copy in any case. It
> >>may be a class reference or a pointer, so calling potentially
> >>non-const methods is probably not safe here, but assignment
> >>shouldn't give you problems.
> >But that's the problem, if e is a dynamic array, then it can
> >potentially be modified through the original reference after being
> >assigned.
> >
> >Ideally, I'd need e to be a reference to an immutable type. But that
> >requires a way of converting an arbitrary type to its immutable form,
> >which I don't know how to do in a generic way.
[...]
> I see. But you can't have a generic copy operation either, due to
> possibly complicated memory model of your program. You'd need a proper
> copy construction for that, but there is no way to check for it's
> correctness in generic type.

True.


> I'd just use constraint to limit a range underlying type to immutable,
> something like:
> 
> template isImmutable(T) {
>     static if( is( T == immutable T ) ) {
>         enum isImmutable = 1;
>     } else {
>         enum isImmutable = 0;
>     }
> }
> ...
> if( isImmutable!(ElementType!T) )
> ...

Hmm. But the problem is that I want to be able to handle something like
File.byLine(). Or perhaps what I really need is just to write a wrapper
around File.readln() that ensures immutability, then I can use
isImmutable() to enforce safety in the algorithm, and just pass the
wrapper when I need to use an underlying File.


T

-- 
MAS = Mana Ada Sistem?


More information about the Digitalmars-d-learn mailing list