Article: Why Const Sucks

Martin Nowak code at dawg.eu
Tue Mar 6 19:23:32 UTC 2018


On Monday, 5 March 2018 at 17:38:52 UTC, H. S. Teoh wrote:
> 	struct Container {
> 		auto opSlice() const {
> 			static struct Result {
> 				private Container impl;
> 				private int n; // internal mutable state
> 				@property bool empty() { ... }
> 				... // rest of range API
> 			}
> 			return Result(this);
> 		}
> 	}

That's definitely a know problem with ranges that hasn't yet been 
solved.
The known workaround is to implement Range and ConstRange.
This idiom could prolly be encapsulated in a safe and generic 
template type to avoid boilerplate.
If we had some covariant mechansim to implement sth. similar to

struct Range(T) if (is(T InoutT == inout))
{
     inout(InoutT) opIndex(size_t i) inout;
}

that would be ideal, but that isn't exactly trivial.
I think we had a better issue for this topic, but here is one 
ticket asking for this feat.
[9983 – inout type can not be used as a parameter for structure 
template](https://issues.dlang.org/show_bug.cgi?id=9983)


More information about the Digitalmars-d-announce mailing list