| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
February 18, 2012 Re: Range question | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
February 18, 2012 Re: Range question | ||||
|---|---|---|---|---|
| ||||
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. T -- Let's eat some disquits while we format the biskettes. | ||||
February 18, 2012 Re: Range question | ||||
|---|---|---|---|---|
| ||||
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.
>
>
> T
>
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. 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) )
...
| ||||
February 18, 2012 Re: Range question | ||||
|---|---|---|---|---|
| ||||
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? | ||||
February 18, 2012 Re: Range question | ||||
|---|---|---|---|---|
| ||||
On Friday, February 17, 2012 22:27:06 H. S. Teoh wrote: > 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. If you need to handle arrays differently, then just use a static if to specialize the appropriate section of code for them. So, something like ElementType!R choose(R, E)(R range) if(isInputRange!R) { Unqual!(ElementType!R) e; foreach(e; range) { if(/* some criterion */) { static if(isDynamicArray!(ElementType!R) && is(Unqual!(ElementType!R) == ElementType!R)) { e = range.front.dup; } else e = range.front; } } return cast(ElementType!R)e; } - Jonathan M Davis | ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply