The line "alias b = that.object" seems to alias this.object:
struct TrivialFunctor (T)
{
T object;
int opCmp ()(auto ref TrivialFunctor!T that)
{
alias a = this.object;
alias b = that.object;
import std.stdio;
writeln (this.object, ` < `, that.object); // 1 < 2
writeln (a, ` < `, b); // 1 < 1
writeln (&(this.object) == &(that.object)); // false, ok
writeln (&a == &b); // true?!
stdout.flush;
if (a < b)
return -1;
else if (a > b)
return 1;
else return 0;
}
auto opBinary (string op)(auto ref TrivialFunctor!T that)
{
return TrivialFunctor (mixin(q{this.object } ~ op ~ q{ that.object}));
}
}
auto trivial_functor (T)(T value)
{
return TrivialFunctor!T (value);
}
void main ()
{
TrivialFunctor!int a = 1.trivial_functor, b = 2.trivial_functor;
auto c = a + b;
assert (a < b && b < c); // fails
}
Comment #1 by b2.temp — 2020-05-24T08:01:04Z
reduced a bit:
---
struct Foo
{
int v;
void test(Foo that) const
{
alias a = this.v;
alias b = that.v;
assert (&a !is &b);
}
}
void main ()
{
Foo a = Foo(1);
Foo b = Foo(2);
a.test(b);
}
---
The problem is that `that.v` is an expression and this should not be allowed as alias RHS. The compiler think that the current `this` is also the one required for `that`.
Comment #2 by b2.temp — 2020-05-24T08:06:20Z
*** Issue 19786 has been marked as a duplicate of this issue. ***
Comment #3 by dlang-bot — 2020-06-24T01:52:39Z
@NilsLankila updated dlang/dmd pull request #11273 "fix issue 3546 - Support for alias of individual array elements" fixing this issue:
- allow alias of member variables and functions
by the way, fix issue 14128
https://github.com/dlang/dmd/pull/11273
Comment #4 by nick — 2023-11-21T20:58:34Z
These declarations all alias the same symbol:
alias a = this.v;
alias b = that.v;
alias c = Foo.v;
alias d = v;
`a` and `b` resolve at compile-time to `v` based on the *type* of `this` (or `that`). The use of an alias of `v` then resolves at runtime to `this.v`.
I agree that `a` and `b` look like they are binding to runtime fields.