Currently defining const and non-const versions of methods in generic code is redundant and error-prone. When calling a non-const method against a const object, D should just generate the const method and attempt to typecheck it. This should cut down significantly on user code and aggravation.
Comment #1 by schveiguy — 2016-08-15T13:22:54Z
One can do this already, though I agree it would be nice if this was the default:
struct S
{
int _x;
int x(this T)() { return _x; }
}
void main()
{
S s;
const S cs;
writeln(s.x, cs.x);
}
Comment #2 by greeenify — 2016-08-15T13:34:23Z
> Currently defining const and non-const versions of methods in generic code is redundant and error-prone.
FWIW isn't inout a solution to a similar problem? In any case inout needs rebranding
Comment #3 by andrei — 2016-08-15T13:50:48Z
(In reply to greenify from comment #2)
> > Currently defining const and non-const versions of methods in generic code is redundant and error-prone.
>
> FWIW isn't inout a solution to a similar problem? In any case inout needs
> rebranding
Deduction is different because there's no qualifier applied if the method is never used on a const object.
Comment #4 by robert.schadek — 2024-12-13T18:49:27Z