So, is there any plan to forbid calling static methods from class instance or is the bug gonna just fix the ambiguity, while still allowing to call static methods if no appropriate non-static one is available?<br><br>Cheers,<br>
Gor.<br><br><div class="gmail_quote">On Tue, Sep 27, 2011 at 7:23 PM, Timon Gehr <span dir="ltr"><<a href="mailto:timon.gehr@gmx.ch">timon.gehr@gmx.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 09/27/2011 05:11 PM, Gor Gyolchanyan wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have a question about static and non-static methods overloading.<br>
I have this module:<br>
<br>
------------------------------<u></u>------------------------------<u></u>--------------------<br>
<br>
/**<br>
   * Dynamic library interfacing module.<br>
   */<br>
module dynlib;<br>
<br>
public:<br>
     /**<br>
       * A lazily loaded cached dynamic library.<br>
       */<br>
     class DynLib<br>
     {<br>
     public:<br>
         alias void* Symbol;<br>
<br>
         /**<br>
           * Loads or returns already loaded dynamic library.<br>
           */<br>
         static DynLib opIndex(in char[] name)<br>
         {<br>
             auto lib = name in _libraries;<br>
             if(lib)<br>
                 return *lib;<br>
             _libraries[name] = new DynLib(name);<br>
             return DynLib[name];<br>
         }<br>
<br>
         /**<br>
           * Loads or returns already loaded symbol from this dynamic<br>
library.<br>
           */<br>
         Symbol opIndex(in char[] name)<br>
         {<br>
             auto sym = name in _symbols;<br>
             if(sym)<br>
                 return *sym;<br>
             version(Windows)<br>
                 _symbols[name] = GetProcAddress(_handle, name.toStringz);<br>
             else version(Posix)<br>
                 _symbols[name] = dlsym(_handle, name.toStringz);<br>
             return DynLib[name];<br>
         }<br>
<br>
         bool empty() @property<br>
         {<br>
             return _handle is null;<br>
         }<br>
<br>
     private:<br>
         alias void* Handle;<br>
<br>
         static DynLib[string] _libraries;<br>
         static Symbol[string] _symbols;<br>
         Handle _handle;<br>
<br>
         this(in char[] name)<br>
         {<br>
             version(Windows)<br>
                 _handle = LoadLibraryA(name.toStringz);<br>
             else version(Posix)<br>
                 _handle = dlopen(name.toStringz, RTLD_NOW);<br>
         }<br>
<br>
         ~this()<br>
         {<br>
             version(Windows)<br>
                 FreeLibrary(_handle);<br>
             version(Posix)<br>
                 dlclose(_handle);<br>
         }<br>
<br>
         unittest<br>
         {<br>
             DynLib dl;<br>
             version(Windows)<br>
                 dl = DynLib["OpenGL32.dll"];<br>
             version(Posix)<br>
                 dl = DynLib["libGL.so"];<br>
             assert(!dl.empty);<br>
<br>
             DynLib.Symbol sym = dl["glClearColor"];<br>
             assert(sym !is null);<br>
         }<br>
     }<br>
<br>
private:<br>
     import std.string: toStringz;<br>
<br>
     version(Windows)<br>
         import core.sys.windows.windows: LoadLibraryA, FreeLibrary,<br>
GetProcAddress;<br>
     else version(Posix)<br>
         import core.sys.posix.dlfcn: dlopen, dlclose, dlsym;<br>
<br>
------------------------------<u></u>------------------------------<u></u>--------------------<br>
<br>
And when i compile this, i get an unexpected error:<br>
<br>
------------------------------<u></u>------------------------------<u></u>--------------------<br>
<br>
dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argument<br>
types:<br>
         ((const(char[])))<br>
matches both:<br>
         dynlib.DynLib.opIndex(in const(char[]) name)<br>
and:<br>
         dynlib.DynLib.opIndex(in const(char[]) name)<br>
dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument<br>
types:<br>
         ((const(char[])))<br>
matches both:<br>
         dynlib.DynLib.opIndex(in const(char[]) name)<br>
and:<br>
         dynlib.DynLib.opIndex(in const(char[]) name)<br>
dynlib.d(39): Error: cannot implicitly convert expression<br>
(opIndex(name)) of type dynlib.DynLib to void*<br>
<br>
------------------------------<u></u>------------------------------<u></u>--------------------<br>
<br>
My point is: How can this be ambiguous, when i explicitly call the<br>
static method from the class name, not the object and when i call a<br>
method from an object, the most obvious choice is the non-static one?<br>
<br>
Cheers,<br>
Gor.<br>
<br>
</blockquote>
<br></div></div>
There as been some discussion about this issue and afaik Steve has filed a bug report. Currently, it is possible to call a static method on a class instance.<br>
</blockquote></div><br>