www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static and non-static method overloading

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--0015174bf06cd2a3b004adedb07c
Content-Type: text/plain; charset=ISO-8859-1

I have a question about static and non-static methods overloading.
I have this module:

--------------------------------------------------------------------------------

/**
  * Dynamic library interfacing module.
  */
module dynlib;

public:
    /**
      * A lazily loaded cached dynamic library.
      */
    class DynLib
    {
    public:
        alias void* Symbol;

        /**
          * Loads or returns already loaded dynamic library.
          */
        static DynLib opIndex(in char[] name)
        {
            auto lib = name in _libraries;
            if(lib)
                return *lib;
            _libraries[name] = new DynLib(name);
            return DynLib[name];
        }

        /**
          * Loads or returns already loaded symbol from this dynamic
library.
          */
        Symbol opIndex(in char[] name)
        {
            auto sym = name in _symbols;
            if(sym)
                return *sym;
            version(Windows)
                _symbols[name] = GetProcAddress(_handle, name.toStringz);
            else version(Posix)
                _symbols[name] = dlsym(_handle, name.toStringz);
            return DynLib[name];
        }

        bool empty()  property
        {
            return _handle is null;
        }

    private:
        alias void* Handle;

        static DynLib[string] _libraries;
        static Symbol[string] _symbols;
        Handle _handle;

        this(in char[] name)
        {
            version(Windows)
                _handle = LoadLibraryA(name.toStringz);
            else version(Posix)
                _handle = dlopen(name.toStringz, RTLD_NOW);
        }

        ~this()
        {
            version(Windows)
                FreeLibrary(_handle);
            version(Posix)
                dlclose(_handle);
        }

        unittest
        {
            DynLib dl;
            version(Windows)
                dl = DynLib["OpenGL32.dll"];
            version(Posix)
                dl = DynLib["libGL.so"];
            assert(!dl.empty);

            DynLib.Symbol sym = dl["glClearColor"];
            assert(sym !is null);
        }
    }

private:
    import std.string: toStringz;

    version(Windows)
        import core.sys.windows.windows: LoadLibraryA, FreeLibrary,
GetProcAddress;
    else version(Posix)
        import core.sys.posix.dlfcn: dlopen, dlclose, dlsym;

--------------------------------------------------------------------------------

And when i compile this, i get an unexpected error:

--------------------------------------------------------------------------------

dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argument
types:
        ((const(char[])))
matches both:
        dynlib.DynLib.opIndex(in const(char[]) name)
and:
        dynlib.DynLib.opIndex(in const(char[]) name)
dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument
types:
        ((const(char[])))
matches both:
        dynlib.DynLib.opIndex(in const(char[]) name)
and:
        dynlib.DynLib.opIndex(in const(char[]) name)
dynlib.d(39): Error: cannot implicitly convert expression (opIndex(name)) of
type dynlib.DynLib to void*

--------------------------------------------------------------------------------

My point is: How can this be ambiguous, when i explicitly call the static
method from the class name, not the object and when i call a method from an
object, the most obvious choice is the non-static one?

Cheers,
Gor.

--0015174bf06cd2a3b004adedb07c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I have a question about static and non-static methods overloading.<br>I hav=
e this module:<br><br>-----------------------------------------------------=
---------------------------<br><br>/**<br>=A0 * Dynamic library interfacing=
 module.<br>
=A0 */<br>module dynlib;<br><br>public:<br>=A0=A0=A0 /**<br>=A0=A0=A0 =A0 *=
 A lazily loaded cached dynamic library.<br>=A0=A0=A0 =A0 */<br>=A0=A0=A0 c=
lass DynLib<br>=A0=A0=A0 {<br>=A0=A0=A0 public:<br>=A0=A0=A0 =A0=A0=A0 alia=
s void* Symbol;<br>=A0=A0=A0 <br>=A0=A0=A0 =A0=A0=A0 /**<br>=A0=A0=A0 =A0=
=A0=A0 =A0 * Loads or returns already loaded dynamic library.<br>
=A0=A0=A0 =A0=A0=A0 =A0 */<br>=A0=A0=A0 =A0=A0=A0 static DynLib opIndex(in =
char[] name)<br>=A0=A0=A0 =A0=A0=A0 {<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 auto=
 lib =3D name in _libraries;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if(lib)<br>=
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return *lib;<br>=A0=A0=A0 =A0=A0=A0=
 =A0=A0=A0 _libraries[name] =3D new DynLib(name);<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return DynLib[name];<br>=A0=A0=A0 =A0=A0=A0 }=
<br>=A0=A0=A0 =A0=A0=A0 <br>=A0=A0=A0 =A0=A0=A0 /**<br>=A0=A0=A0 =A0=A0=A0 =
=A0 * Loads or returns already loaded symbol from this dynamic library.<br>=
=A0=A0=A0 =A0=A0=A0 =A0 */<br>=A0=A0=A0 =A0=A0=A0 Symbol opIndex(in char[] =
name)<br>=A0=A0=A0 =A0=A0=A0 {<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 auto sym =3D name in _symbols;<br>=A0=A0=A0 =
=A0=A0=A0 =A0=A0=A0 if(sym)<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 retu=
rn *sym;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 version(Windows)<br>=A0=A0=A0 =A0=
=A0=A0 =A0=A0=A0 =A0=A0=A0 _symbols[name] =3D GetProcAddress(_handle, name.=
toStringz);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 else version(Posix)<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 _symbols[name] =3D dlsym(_handle, n=
ame.toStringz);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return DynLib[name];=A0=A0=
=A0 <br>=A0=A0=A0 =A0=A0=A0 }<br>=A0=A0=A0 =A0=A0=A0 <br>=A0=A0=A0 =A0=A0=
=A0 bool empty()  property<br>=A0=A0=A0 =A0=A0=A0 {<br>=A0=A0=A0 =A0=A0=A0 =
=A0=A0=A0 return _handle is null;<br>=A0=A0=A0 =A0=A0=A0 }<br>
<br>=A0=A0=A0 private:<br>=A0=A0=A0 =A0=A0=A0 alias void* Handle;<br>=A0=A0=
=A0 =A0=A0=A0 =A0=A0=A0 <br>=A0=A0=A0 =A0=A0=A0 static DynLib[string] _libr=
aries;<br>=A0=A0=A0 =A0=A0=A0 static Symbol[string] _symbols;<br>=A0=A0=A0 =
=A0=A0=A0 Handle _handle;<br>=A0=A0=A0 =A0=A0=A0 <br>=A0=A0=A0 =A0=A0=A0 th=
is(in char[] name)<br>
=A0=A0=A0 =A0=A0=A0 {<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 version(Windows)<br>=
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 _handle =3D LoadLibraryA(name.toStr=
ingz);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 else version(Posix)<br>=A0=A0=A0 =
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 _handle =3D dlopen(name.toStringz, RTLD_NOW);=
<br>=A0=A0=A0 =A0=A0=A0 }<br>=A0=A0=A0 =A0=A0=A0 <br>
=A0=A0=A0 =A0=A0=A0 ~this()<br>=A0=A0=A0 =A0=A0=A0 {<br>=A0=A0=A0 =A0=A0=A0=
 =A0=A0=A0 version(Windows)<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 Free=
Library(_handle);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 version(Posix)<br>=A0=A0=
=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 dlclose(_handle);<br>=A0=A0=A0 =A0=A0=A0 =
}<br>=A0=A0=A0 =A0=A0=A0 <br>=A0=A0=A0 =A0=A0=A0 unittest<br>=A0=A0=A0 =A0=
=A0=A0 {<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 DynLib dl;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 v=
ersion(Windows)<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 dl =3D DynLib[&q=
uot;OpenGL32.dll&quot;];<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 version(Posix)<br=
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 dl =3D DynLib[&quot;libGL.so&quot;=

<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 DynLib.Symbol sym =3D dl[&quot;glClearCol= or&quot;];<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 assert(sym !is null);<br>=A0=A0= =A0 =A0=A0=A0 }<br>=A0=A0=A0 }<br>=A0=A0=A0 <br>private:<br>=A0=A0=A0 impor= t std.string: toStringz;<br><br>=A0=A0=A0 version(Windows)<br>=A0=A0=A0 =A0= =A0=A0 import core.sys.windows.windows: LoadLibraryA, FreeLibrary, GetProcA= ddress;<br> =A0=A0=A0 else version(Posix)<br>=A0=A0=A0 =A0=A0=A0 import core.sys.posix.= dlfcn: dlopen, dlclose, dlsym;<br><br>-------------------------------------= -------------------------------------------<br><br>And when i compile this,= i get an unexpected error:<br> <br>-----------------------------------------------------------------------= ---------<br> <br>dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argumen= t types:<br>=A0=A0=A0=A0=A0=A0=A0 ((const(char[])))<br>matches both:<br>=A0= =A0=A0=A0=A0=A0=A0 dynlib.DynLib.opIndex(in const(char[]) name)<br>and:<br>= =A0=A0=A0=A0=A0=A0=A0 dynlib.DynLib.opIndex(in const(char[]) name)<br> dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument ty= pes:<br>=A0=A0=A0=A0=A0=A0=A0 ((const(char[])))<br>matches both:<br>=A0=A0= =A0=A0=A0=A0=A0 dynlib.DynLib.opIndex(in const(char[]) name)<br>and:<br>=A0= =A0=A0=A0=A0=A0=A0 dynlib.DynLib.opIndex(in const(char[]) name)<br> dynlib.d(39): Error: cannot implicitly convert expression (opIndex(name)) o= f type dynlib.DynLib to void*<br><br>--------------------------------------= ------------------------------------------<br> <br>My point is: How can this be ambiguous, when i explicitly call the stat= ic method from the class name, not the object and when i call a method from= an object, the most obvious choice is the non-static one?<br><br>Cheers,<b= r> Gor.<br><br> --0015174bf06cd2a3b004adedb07c--
Sep 27 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/27/2011 05:11 PM, Gor Gyolchanyan wrote:
 I have a question about static and non-static methods overloading.
 I have this module:

 --------------------------------------------------------------------------------

 /**
    * Dynamic library interfacing module.
    */
 module dynlib;

 public:
      /**
        * A lazily loaded cached dynamic library.
        */
      class DynLib
      {
      public:
          alias void* Symbol;

          /**
            * Loads or returns already loaded dynamic library.
            */
          static DynLib opIndex(in char[] name)
          {
              auto lib = name in _libraries;
              if(lib)
                  return *lib;
              _libraries[name] = new DynLib(name);
              return DynLib[name];
          }

          /**
            * Loads or returns already loaded symbol from this dynamic
 library.
            */
          Symbol opIndex(in char[] name)
          {
              auto sym = name in _symbols;
              if(sym)
                  return *sym;
              version(Windows)
                  _symbols[name] = GetProcAddress(_handle, name.toStringz);
              else version(Posix)
                  _symbols[name] = dlsym(_handle, name.toStringz);
              return DynLib[name];
          }

          bool empty()  property
          {
              return _handle is null;
          }

      private:
          alias void* Handle;

          static DynLib[string] _libraries;
          static Symbol[string] _symbols;
          Handle _handle;

          this(in char[] name)
          {
              version(Windows)
                  _handle = LoadLibraryA(name.toStringz);
              else version(Posix)
                  _handle = dlopen(name.toStringz, RTLD_NOW);
          }

          ~this()
          {
              version(Windows)
                  FreeLibrary(_handle);
              version(Posix)
                  dlclose(_handle);
          }

          unittest
          {
              DynLib dl;
              version(Windows)
                  dl = DynLib["OpenGL32.dll"];
              version(Posix)
                  dl = DynLib["libGL.so"];
              assert(!dl.empty);

              DynLib.Symbol sym = dl["glClearColor"];
              assert(sym !is null);
          }
      }

 private:
      import std.string: toStringz;

      version(Windows)
          import core.sys.windows.windows: LoadLibraryA, FreeLibrary,
 GetProcAddress;
      else version(Posix)
          import core.sys.posix.dlfcn: dlopen, dlclose, dlsym;

 --------------------------------------------------------------------------------

 And when i compile this, i get an unexpected error:

 --------------------------------------------------------------------------------

 dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argument
 types:
          ((const(char[])))
 matches both:
          dynlib.DynLib.opIndex(in const(char[]) name)
 and:
          dynlib.DynLib.opIndex(in const(char[]) name)
 dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument
 types:
          ((const(char[])))
 matches both:
          dynlib.DynLib.opIndex(in const(char[]) name)
 and:
          dynlib.DynLib.opIndex(in const(char[]) name)
 dynlib.d(39): Error: cannot implicitly convert expression
 (opIndex(name)) of type dynlib.DynLib to void*

 --------------------------------------------------------------------------------

 My point is: How can this be ambiguous, when i explicitly call the
 static method from the class name, not the object and when i call a
 method from an object, the most obvious choice is the non-static one?

 Cheers,
 Gor.

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.
Sep 27 2011
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--0015174c462cdf8f9d04adee11bf
Content-Type: text/plain; charset=ISO-8859-1

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?

Cheers,
Gor.

On Tue, Sep 27, 2011 at 7:23 PM, Timon Gehr <timon.gehr gmx.ch> wrote:

 On 09/27/2011 05:11 PM, Gor Gyolchanyan wrote:

 I have a question about static and non-static methods overloading.
 I have this module:

 ------------------------------**------------------------------**
 --------------------

 /**
   * Dynamic library interfacing module.
   */
 module dynlib;

 public:
     /**
       * A lazily loaded cached dynamic library.
       */
     class DynLib
     {
     public:
         alias void* Symbol;

         /**
           * Loads or returns already loaded dynamic library.
           */
         static DynLib opIndex(in char[] name)
         {
             auto lib = name in _libraries;
             if(lib)
                 return *lib;
             _libraries[name] = new DynLib(name);
             return DynLib[name];
         }

         /**
           * Loads or returns already loaded symbol from this dynamic
 library.
           */
         Symbol opIndex(in char[] name)
         {
             auto sym = name in _symbols;
             if(sym)
                 return *sym;
             version(Windows)
                 _symbols[name] = GetProcAddress(_handle, name.toStringz);
             else version(Posix)
                 _symbols[name] = dlsym(_handle, name.toStringz);
             return DynLib[name];
         }

         bool empty()  property
         {
             return _handle is null;
         }

     private:
         alias void* Handle;

         static DynLib[string] _libraries;
         static Symbol[string] _symbols;
         Handle _handle;

         this(in char[] name)
         {
             version(Windows)
                 _handle = LoadLibraryA(name.toStringz);
             else version(Posix)
                 _handle = dlopen(name.toStringz, RTLD_NOW);
         }

         ~this()
         {
             version(Windows)
                 FreeLibrary(_handle);
             version(Posix)
                 dlclose(_handle);
         }

         unittest
         {
             DynLib dl;
             version(Windows)
                 dl = DynLib["OpenGL32.dll"];
             version(Posix)
                 dl = DynLib["libGL.so"];
             assert(!dl.empty);

             DynLib.Symbol sym = dl["glClearColor"];
             assert(sym !is null);
         }
     }

 private:
     import std.string: toStringz;

     version(Windows)
         import core.sys.windows.windows: LoadLibraryA, FreeLibrary,
 GetProcAddress;
     else version(Posix)
         import core.sys.posix.dlfcn: dlopen, dlclose, dlsym;

 ------------------------------**------------------------------**
 --------------------

 And when i compile this, i get an unexpected error:

 ------------------------------**------------------------------**
 --------------------

 dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argument
 types:
         ((const(char[])))
 matches both:
         dynlib.DynLib.opIndex(in const(char[]) name)
 and:
         dynlib.DynLib.opIndex(in const(char[]) name)
 dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument
 types:
         ((const(char[])))
 matches both:
         dynlib.DynLib.opIndex(in const(char[]) name)
 and:
         dynlib.DynLib.opIndex(in const(char[]) name)
 dynlib.d(39): Error: cannot implicitly convert expression
 (opIndex(name)) of type dynlib.DynLib to void*

 ------------------------------**------------------------------**
 --------------------

 My point is: How can this be ambiguous, when i explicitly call the
 static method from the class name, not the object and when i call a
 method from an object, the most obvious choice is the non-static one?

 Cheers,
 Gor.

bug report. Currently, it is possible to call a static method on a class instance.

--0015174c462cdf8f9d04adee11bf Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 st= atic methods if no appropriate non-static one is available?<br><br>Cheers,<= br> Gor.<br><br><div class=3D"gmail_quote">On Tue, Sep 27, 2011 at 7:23 PM, Tim= on Gehr <span dir=3D"ltr">&lt;<a href=3D"mailto:timon.gehr gmx.ch">timon.ge= hr gmx.ch</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div><div></div><div class=3D"h5">On 09/27/2011 05:11 PM, Gor Gyolchanyan w= rote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #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> =A0 * Dynamic library interfacing module.<br> =A0 */<br> module dynlib;<br> <br> public:<br> =A0 =A0 /**<br> =A0 =A0 =A0 * A lazily loaded cached dynamic library.<br> =A0 =A0 =A0 */<br> =A0 =A0 class DynLib<br> =A0 =A0 {<br> =A0 =A0 public:<br> =A0 =A0 =A0 =A0 alias void* Symbol;<br> <br> =A0 =A0 =A0 =A0 /**<br> =A0 =A0 =A0 =A0 =A0 * Loads or returns already loaded dynamic library.<br> =A0 =A0 =A0 =A0 =A0 */<br> =A0 =A0 =A0 =A0 static DynLib opIndex(in char[] name)<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 auto lib =3D name in _libraries;<br> =A0 =A0 =A0 =A0 =A0 =A0 if(lib)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return *lib;<br> =A0 =A0 =A0 =A0 =A0 =A0 _libraries[name] =3D new DynLib(name);<br> =A0 =A0 =A0 =A0 =A0 =A0 return DynLib[name];<br> =A0 =A0 =A0 =A0 }<br> <br> =A0 =A0 =A0 =A0 /**<br> =A0 =A0 =A0 =A0 =A0 * Loads or returns already loaded symbol from this dyn= amic<br> library.<br> =A0 =A0 =A0 =A0 =A0 */<br> =A0 =A0 =A0 =A0 Symbol opIndex(in char[] name)<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 auto sym =3D name in _symbols;<br> =A0 =A0 =A0 =A0 =A0 =A0 if(sym)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return *sym;<br> =A0 =A0 =A0 =A0 =A0 =A0 version(Windows)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _symbols[name] =3D GetProcAddress(_handle,= name.toStringz);<br> =A0 =A0 =A0 =A0 =A0 =A0 else version(Posix)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _symbols[name] =3D dlsym(_handle, name.toS= tringz);<br> =A0 =A0 =A0 =A0 =A0 =A0 return DynLib[name];<br> =A0 =A0 =A0 =A0 }<br> <br> =A0 =A0 =A0 =A0 bool empty() property<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 return _handle is null;<br> =A0 =A0 =A0 =A0 }<br> <br> =A0 =A0 private:<br> =A0 =A0 =A0 =A0 alias void* Handle;<br> <br> =A0 =A0 =A0 =A0 static DynLib[string] _libraries;<br> =A0 =A0 =A0 =A0 static Symbol[string] _symbols;<br> =A0 =A0 =A0 =A0 Handle _handle;<br> <br> =A0 =A0 =A0 =A0 this(in char[] name)<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 version(Windows)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _handle =3D LoadLibraryA(name.toStringz);<= br> =A0 =A0 =A0 =A0 =A0 =A0 else version(Posix)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 _handle =3D dlopen(name.toStringz, RTLD_NO= W);<br> =A0 =A0 =A0 =A0 }<br> <br> =A0 =A0 =A0 =A0 ~this()<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 version(Windows)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FreeLibrary(_handle);<br> =A0 =A0 =A0 =A0 =A0 =A0 version(Posix)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 dlclose(_handle);<br> =A0 =A0 =A0 =A0 }<br> <br> =A0 =A0 =A0 =A0 unittest<br> =A0 =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 =A0 DynLib dl;<br> =A0 =A0 =A0 =A0 =A0 =A0 version(Windows)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 dl =3D DynLib[&quot;OpenGL32.dll&quot;];<b= r> =A0 =A0 =A0 =A0 =A0 =A0 version(Posix)<br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 dl =3D DynLib[&quot;libGL.so&quot;];<br> =A0 =A0 =A0 =A0 =A0 =A0 assert(!dl.empty);<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 DynLib.Symbol sym =3D dl[&quot;glClearColor&quot;]= ;<br> =A0 =A0 =A0 =A0 =A0 =A0 assert(sym !is null);<br> =A0 =A0 =A0 =A0 }<br> =A0 =A0 }<br> <br> private:<br> =A0 =A0 import std.string: toStringz;<br> <br> =A0 =A0 version(Windows)<br> =A0 =A0 =A0 =A0 import core.sys.windows.windows: LoadLibraryA, FreeLibrary= ,<br> GetProcAddress;<br> =A0 =A0 else version(Posix)<br> =A0 =A0 =A0 =A0 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=

=A0 =A0 =A0 =A0 ((const(char[])))<br> matches both:<br> =A0 =A0 =A0 =A0 dynlib.DynLib.opIndex(in const(char[]) name)<br> and:<br> =A0 =A0 =A0 =A0 dynlib.DynLib.opIndex(in const(char[]) name)<br> dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument<br=

=A0 =A0 =A0 =A0 ((const(char[])))<br> matches both:<br> =A0 =A0 =A0 =A0 dynlib.DynLib.opIndex(in const(char[]) name)<br> and:<br> =A0 =A0 =A0 =A0 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 in= stance.<br> </blockquote></div><br> --0015174c462cdf8f9d04adee11bf--
Sep 27 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 27 Sep 2011 11:38:40 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan gmail.com> wrote:

 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?

It's an enhancement request, no idea if it will make it in. Feel free to comment: http://d.puremagic.com/issues/show_bug.cgi?id=6579 -Steve
Sep 27 2011