getting rid of immutable (or const)

berni someone at somewhere.com
Thu Sep 5 08:44:35 UTC 2019


On Thursday, 5 September 2019 at 08:16:08 UTC, Daniel Kozak wrote:
> in this case you can just use:
>
> auto q = cast()p.x;

Ahh, great! :-)

But that directly gets me to the next question:

> import std.stdio;
> 
> void main()
> {
>     Point[] q = [Point(1),Point(3),Point(2)];
> 
>     import std.algorithm.searching: minElement;
>     writeln(q.minElement!(a=>a.x).x);
> }
> 
> struct Point
> {
>    @property immutable long x;
> }

This doesn't compile:

> /usr/include/dmd/phobos/std/algorithm/searching.d(1365): Error: 
> cannot modify struct extremeElement Point with immutable members
> /usr/include/dmd/phobos/std/algorithm/searching.d(1307): Error: 
> template instance `test.main.extremum!(__lambda1, "a < b", 
> Point[], Point)` error instantiating
> /usr/include/dmd/phobos/std/algorithm/searching.d(3445):        
> instantiated from here: extremum!(__lambda1, "a < b", Point[])
> test.d(8):        instantiated from here: minElement!((a) => 
> a.x, Point[])

Any idea, how to get around this?


More information about the Digitalmars-d-learn mailing list