-
-
Notifications
You must be signed in to change notification settings - Fork 742
Fix constness of array.d #2631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix constness of array.d #2631
Conversation
} | ||
|
||
@property ref inout(T) back() inout | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if elements are immutable? Then things like arr[].filter(x > 0).array
degrade to const element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not related to DmitryOlshansky's comment.)
Looks like I've missed an else
here. There should be no else
s to the static if (isMutable!A)
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add another
else static if (is(T == immutable))
{
@property ref immutable(T) front() const
{
version (assert) if (empty) throw new RangeError();
return _outer[_a];
}
@property ref immutable(T) back() const
{
version (assert) if (empty) throw new RangeError();
return _outer[_b - 1];
}
}
then?
|
I don't think adding all this duplicated code is a nice solution to this problem. The fundamental issue is that we have conversions for |
JakobOvrum wrote:
We could get around the breakage by renaming the range template to |
ad0fce9
to
6fd1646
Compare
@aG0aep6G I guess you mean alias Range(T) = RangeT!(Array!T); right? |
(...) I meant And it has to go into Array, because that's where it's been the whole time. The point is that |
I made a pull request against your branch with what I mean: nordlow#2 |
Sigh, I got back the same compilation errors after merging #2643. I don't understand what I did wrong. How do I ignore edits from pull 2643 when I merge or rebase? |
What errors? What do you want to do? What did you do? |
My rebase gave merge conflicts beyond what I could resolve because someone did a revert of previous work on the constness of array.d at #2643. I start over and see what you guys think this time. |
e65acca
to
81eb394
Compare
When throwing away everything from #2643, you also deleted this test: https://github.com/Dicebot/phobos/commit/1149972daca5290ed401dcd0a03f3a7eb5c5074c. Better put it back in. |
81eb394
to
8a14120
Compare
Done. BTW: Don't we need a specific test for immutable Arrays and/or Ranges? |
I have a suggestion that's rather invasive. So I made a pull request: nordlow#3 The commit message:
This gets us rid of quite some duplication. Also adds tests for const/immutable Arrays/Ranges. |
True, but there doesn't seem to be an alternative solution. |
I'd really like to have a solid solution to this problem as it's a recurring problem and resorting to Range, ConstRange and ImmutableRange ( |
8a14120
to
4bb3cd7
Compare
Everything's here. I'm closing nordlow#3. Maybe you didn't pull it through Github or something. |
Great. Ready to merge? |
} | ||
|
||
void opSliceAssign(T value) | ||
RangeT!(const(A)) opSlice() const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why these are const instead of using inout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing to inout
triggers a compiler bug and errors as
array.d(235,22): Error: variable std.container.array.Array!int.Array.RangeT!(inout(Array!int)).RangeT._outer_ only parameters or stack based variables can be inout
array.d(235,22): Error: variable std.container.array.Array!int.Array.RangeT!(inout(const(Array!int))).RangeT._outer_ only parameters or stack based variables can be inout
array.d(333,9): Error: template instance std.container.array.Array!int.Array.RangeT!(inout(const(Array!int))) error instantiating
array.d(328,9): instantiated from here: RangeT!(inout(Array!int))
array.d(386,19): instantiated from here: RangeT!(Array!int)
array.d(898,5): instantiated from here: Array!int
array.d(333,9): Error: template instance std.container.array.Array!int.Array.RangeT!(const(Array!int)) error instantiating
array.d(386,19): instantiated from here: RangeT!(Array!int)
array.d(898,5): instantiated from here: Array!int
array.d(927,5): Error: static assert (!true) is false
All the positional insert and remove methods now only work when given mutable positions (i.e. |
Defines the container's primary range, which is a random-access range. | ||
*/ | ||
static struct Range | ||
private static struct RangeT(A) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If doesn't depend on T move it out of Array strict.
No need to spread the bloat by putting a template in a template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there are different views about this. Do @MartinNowak @Dicebot @yebblies @JakobOvrum agree on this? I've already made this refactoring but some reviewer wanted me to revert it. I don't remember who.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I with @DmitryOlshansky here - unnecessary nesting of templates creates potential combinatoric template bloat and no D compiler is currently capable of merging identical binary blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's also a little confusing from a readability perspective.
I really wish this type didn't have to be a template to solve this problem, but I don't know of any solutions myself...
Looking good overall |
9bcd503
to
0fdf081
Compare
And rebased. |
0fdf081
to
744c704
Compare
Moved |
On Wed, Nov 26, 2014 at 8:06 PM, JakobOvrum notifications@github.com
This hasn't changed, has it? They took a (mutable) Range before, too. Maybe Like before, one has to obtain a Range from a mutable Array for mutating |
I'm not sure what is the best choice here regarding mutablity of
I vote for a separate pull. |
Ping again? |
Is there something you expect me to add here? If the problem is mutability issues could you please show me a unittest that currently fails that should pass. I'm not skilled enough in Phobos policies to decide whether positional inserts and removes should require mutability of |
Ping. |
|
||
public import std.container.util; | ||
|
||
private static struct RangeT(A) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static
for structs is a no-op at module level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
I'd still prefer a more general solution involving conversions between |
I'll merge it in a day or so unless someone finds new evidence against it until then. |
Quite a few commits here, please squash. |
squash |
after that green light @JakobOvrum |
f942e81
to
14d928e
Compare
Done. I did an interactive rebase. I hope that is what you wanted me to do. |
Auto-merge toggled on |
Thanks to @aG0aep6G this works as far as I can see.