Thread overview | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 05, 2019 getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
I still struggle with the concept of immutable and const:
> import std.stdio;
>
> void main()
> {
> auto p = Point(3);
> auto q = p.x;
> writeln(typeof(q).stringof);
> }
>
> struct Point
> {
> @property immutable long x;
> }
The type of q is immutable(long). But I need a mutable q. I found two ways:
a) long q = p.x;
b) auto q = cast(long)p.x;
Either way I've to specify the type "long" which I dislike (here it's not a real burdon, but with more complicated types it might be). Is there a way, to make q mutable without having to write the type explicitly?
|
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | On Thu, Sep 5, 2019 at 9:55 AM berni via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > > I still struggle with the concept of immutable and const: > > > import std.stdio; > > > > void main() > > { > > auto p = Point(3); > > auto q = p.x; > > writeln(typeof(q).stringof); > > } > > > > struct Point > > { > > @property immutable long x; > > } > > The type of q is immutable(long). But I need a mutable q. I found > two ways: > > a) long q = p.x; > b) auto q = cast(long)p.x; > > Either way I've to specify the type "long" which I dislike (here it's not a real burdon, but with more complicated types it might be). Is there a way, to make q mutable without having to write the type explicitly? in this case you can just use: auto q = cast()p.x; |
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Thursday, 5 September 2019 at 08:16:08 UTC, Daniel Kozak wrote:
> On Thu, Sep 5, 2019 at 9:55 AM berni via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>>
>> I still struggle with the concept of immutable and const:
>>
>> > import std.stdio;
>> >
>> > void main()
>> > {
>> > auto p = Point(3);
>> > auto q = p.x;
>> > writeln(typeof(q).stringof);
>> > }
>> >
>> > struct Point
>> > {
>> > @property immutable long x;
>> > }
>>
>> The type of q is immutable(long). But I need a mutable q. I found
>> two ways:
>>
>> a) long q = p.x;
>> b) auto q = cast(long)p.x;
>>
>> Either way I've to specify the type "long" which I dislike (here it's not a real burdon, but with more complicated types it might be). Is there a way, to make q mutable without having to write the type explicitly?
>
> in this case you can just use:
>
> auto q = cast()p.x;
or:
auto q = p.x + 0;
|
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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? |
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | On Thursday, 5 September 2019 at 08:44:35 UTC, berni wrote:
> This doesn't compile:
>
> [...]
>
> Any idea, how to get around this?
Found the answer myself: q.map!(a=>a.x).minElement; :-)
|
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | On Thursday, 5 September 2019 at 08:56:42 UTC, berni wrote: > [..] And one more question: > import std.algorithm: reverse; > writeln(q.reverse); Here the compiler complains with: > test.d(8): Error: template std.algorithm.mutation.reverse cannot deduce function from argument types !()(Point[]), candidates are: > /usr/include/dmd/phobos/std/algorithm/mutation.d(2483): std.algorithm.mutation.reverse(Range)(Range r) if (isBidirectionalRange!Range && (hasSwappableElements!Range || hasAssignableElements!Range && hasLength!Range && isRandomAccessRange!Range || isNarrowString!Range && isAssignable!(ElementType!Range))) I allready tried to use q.dup.reverse but that didn't work either. How to get this working? (I hope I don't annoy you by asking that much questions, but I've got the feeling, that I've got only two choices: To shy away from using immutable (like I did in the last three years) or ask a lot of questions in the hope of understanding what's going on... |
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | On Thursday, 5 September 2019 at 09:07:30 UTC, berni wrote: >> import std.algorithm: reverse; >> writeln(q.reverse); > > How to get this working? (I hope I don't annoy you by asking that much questions, but I've got the feeling, that I've got only two choices: To shy away from using immutable (like I did in the last three years) or ask a lot of questions in the hope of understanding what's going on... https://dlang.org/library/std/range/retro.html Difference is, retro lazily iterates in reverse order, while reverse eagerly reverses in-place. Don't worry about asking questions - it's a good way to learn, and we like helping. :) Immutable is not very well supported everywhere in the library, sadly. It seems an important building block would be something like Reassignable!T, which would hold a struct with immutable members, and still be reassignable with different values. -- Simen |
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On Thursday, 5 September 2019 at 10:47:56 UTC, Simen Kjærås wrote: > https://dlang.org/library/std/range/retro.html Yeah, that worked. Thanks. :-) > Don't worry about asking questions OK. Then here's the next one: > Point[long] q; > > q[1] = Point(3); Leads to: >test.d(7): Error: cannot modify struct q[1L] Point with immutable members |
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | 05.09.2019 14:17, berni пишет:
>> Point[long] q;
>>
>> q[1] = Point(3);
>
> Leads to:
>
>> test.d(7): Error: cannot modify struct q[1L] Point with immutable members
>
>
But why do you try to modify immutable data? What is your point? Could you describe you use case?
|
September 05, 2019 Re: getting rid of immutable (or const) | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On Thursday, 5 September 2019 at 11:22:15 UTC, drug wrote:
> 05.09.2019 14:17, berni пишет:
>>> Point[long] q;
>>>
>>> q[1] = Point(3);
>>
>> Leads to:
>>
>>> test.d(7): Error: cannot modify struct q[1L] Point with immutable members
>>
>>
> But why do you try to modify immutable data? What is your point? Could you describe you use case?
That's probably, what I don't understand. I've got a Point, which should not be modified. I put it in a container (q) and later I get it out there again. It should still be the same Point as before. I modify the container, not the Point, don't I?
|
Copyright © 1999-2021 by the D Language Foundation