$ dmd mixinstruct.d
mixinstruct.d(15): function mixinstruct.S.attr (int) does not match parameter types ()
mixinstruct.d(15): Error: s.attr can only be called on a mutable object, not const(S)
mixinstruct.d(15): Error: expected 1 arguments, not 0
mixinstruct.d(15): Error: expression s.attr() is void and has no value
$ cat mixinstruct.d
==============================================================
template T() {
int m_attr;
const {int attr() {return m_attr;}} // reader
}
struct S {
mixin T;
void attr(int n) {m_attr = n;} // writer
}
int main() {
const S s;
int r;
r = s.attr(); // why this resolve to the writer? but not the reader?
return r;
}
==============================================================
Comment #1 by smjg — 2008-11-24T06:45:43Z
On DMD 2.019 Windows, the errors are the same, except at line 14.
Comment #2 by pompei2 — 2011-12-17T17:05:10Z
This is still an issue in dmd 2.057, tested with the following code:
mixin template EagerSingleton()
{
private this()
{
instance = this;
}
private static typeof(this) instance;
}
class NetworkPacketQueue
{
mixin EagerSingleton;
public this(string bla)
{
this();
}
}
void main()
{
new NetworkPacketQueue("Hey");
}
Comment #3 by k.hara.pg — 2011-12-17T18:12:05Z
This is not an issue.
http://d-programming-language.org/template-mixin.html
> Mixin Scope
>
> The declarations in a mixin are ‘imported’ into the surrounding scope. If
> the name of a declaration in a mixin is the same as a declaration in the
> surrounding scope, the surrounding declaration overrides the mixin one:
^^^^^^^^^
If you want to merge overloads, you can add alias declaration to do it.
template T() {
int m_attr;
const {int attr() {return m_attr;}}
}
struct S {
mixin T t;
void attr(int n) {m_attr = n;}
alias t.attr attr; // Merge overloads
}
int main() {
const S s;
int r;
r = s.attr();
return r;
}
Comment #4 by simen.kjaras — 2019-07-24T12:02:00Z
*** Issue 20080 has been marked as a duplicate of this issue. ***