D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16473 - operator overloading is broken
Summary: operator overloading is broken
Status: RESOLVED MOVED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-06 13:26 UTC by Илья Ярошенко
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Илья Ярошенко 2016-09-06 13:26:11 UTC
-------------------------------------------
import std.experimental.ndslice;

void main()
{
    auto sl = new double[10].sliced(2, 5);
	auto d = -sl[0, 1];
}
-------------------------------------------
/d921/f269.d(6): Error: template std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary cannot deduce function from argument types !("-")(int, int), candidates are:
/opt/compilers/dmd2/include/std/experimental/ndslice/slice.d(1980):        std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary(string op, Indexes...)(Indexes _indexes) if (isFullPureIndex!Indexes && (op == "++" || op == "--"))
/opt/compilers/dmd2/include/std/experimental/ndslice/slice.d(2008):        std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary(string op, Slices...)(Slices slices) if (isFullPureSlice!Slices && (op == "++" || op == "--"))
-------------

The are two prototypes in Slice. Both with if (op == "++" || op == "--") condition.
So, I don't think we need to add all opIndexUnary variants.
Instead, if expression -sl[0, 1] can not be expanded with opIndexUnary, then is should be expanded with -(sl.opIndex(0, 1)).
Comment 1 Ryan 2016-09-25 01:50:48 UTC
I was able to fix it by adding `-` to the list of accepted strings in the if statement. So now the function signature looks like this
--------------------
auto ref opIndexUnary(string op, Indexes...)(Indexes _indexes)
            if (isFullPureIndex!Indexes && (op == `++` || op == `--` || op == `-`))
--------------------

This works for the case reported in the original ticket. Not sure what to do about the overload that takes slices. The semantically typical thing to do would be return a new slice with a new array underneath it that has the negated values, so as to not modify the underlying array. But that would cause an allocation.
Comment 2 Илья Ярошенко 2016-09-25 12:07:38 UTC
(In reply to Ryan from comment #1)
> I was able to fix it by adding `-` to the list of accepted strings in the if
> statement. So now the function signature looks like this
> --------------------
> auto ref opIndexUnary(string op, Indexes...)(Indexes _indexes)
>             if (isFullPureIndex!Indexes && (op == `++` || op == `--` || op
> == `-`))
> --------------------
> 
> This works for the case reported in the original ticket. Not sure what to do
> about the overload that takes slices. The semantically typical thing to do
> would be return a new slice with a new array underneath it that has the
> negated values, so as to not modify the underlying array. But that would
> cause an allocation.

This is a workaround, which generate more template bloat. We need DMD FE bug fix
Comment 3 github-bugzilla 2016-09-30 15:19:38 UTC
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/545dfd08d705574928eef96bffb914b646aa74e0
workaround for Issue 16473

https://github.com/dlang/phobos/commit/302632968f164867e4aa08f1cf8b1429a43cf2a6
Merge pull request #4820 from 9il/workaround16473

workaround for Issue 16473