D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2157 - [GSoC] mixin struct, function overload
Summary: [GSoC] mixin struct, function overload
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
: 20080 (view as issue list)
Depends on:
Blocks:
 
Reported: 2008-06-21 00:32 UTC by sa
Modified: 2019-07-24 12:02 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description sa 2008-06-21 00:32:10 UTC
$ 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 Stewart Gordon 2008-11-24 06:45:43 UTC
On DMD 2.019 Windows, the errors are the same, except at line 14.
Comment 2 pompei2 2011-12-17 17:05:10 UTC
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 Kenji Hara 2011-12-17 18:12:05 UTC
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 Simen Kjaeraas 2019-07-24 12:02:00 UTC
*** Issue 20080 has been marked as a duplicate of this issue. ***