www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Adding overloaded methods

reply BLM <blm768 gmail.com> writes:
I'm working on a project where I'm using overloaded virtual methods, and I've
run into a challenge with overload sets.

My code looks something like this:

class Base {
 void get(ubyte b) {};
}

class Derived: Base {
 //alias Base.get get;
 void get(string s) {};
}

I've tried using an alias declaration to combine the two classes' overload
sets in the derived class, but DMD complains that the alias conflicts with
get(string). Is there some reasonably elegant way around this, or is it a
limitation of the language?
Feb 21 2012
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, February 22, 2012 02:21:43 BLM wrote:
 I'm working on a project where I'm using overloaded virtual methods, and
 I've run into a challenge with overload sets.
 
 My code looks something like this:
 
 class Base {
 void get(ubyte b) {};
 }
 
 class Derived: Base {
 //alias Base.get get;
 void get(string s) {};
 }
 
 I've tried using an alias declaration to combine the two classes' overload
 sets in the derived class, but DMD complains that the alias conflicts with
 get(string). Is there some reasonably elegant way around this, or is it a
 limitation of the language?
Hmm. That's how you're _supposed_ to do it. Sounds like a bug. http://dlang.org/function.html It may be related to the fact that you don't have override on the derived class's get though. You're really supposed to, but it hasn't been promoted to an error yet (it's enabled with -w until it is). Try that and see if it fixes it. - Jonathan M Davis
Feb 21 2012
next sibling parent reply BLM <blm768 gmail.com> writes:
I tried using override and it complained that the functions weren't overriding
anything. I think that the main problem is that the alias solution was designed
to
allow derived classes to use overloads that had already been defined in the base
class, not for the derived classes to add _new_ overloads. This might be good
material for an enhancement request.
Feb 21 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, February 22, 2012 02:50:41 BLM wrote:
 I tried using override and it complained that the functions weren't
 overriding anything. I think that the main problem is that the alias
 solution was designed to allow derived classes to use overloads that had
 already been defined in the base class, not for the derived classes to add
 _new_ overloads. This might be good material for an enhancement request.
I'd advise reporting it as a bug rather than an enhancement request. The whole point of using the alias is to bring all of the base class' overloads into the derived class' overload set. It shouldn't matter whether the derived class is adding overloads or just overriding a subset of the base class' overloads. If the compiler devs really think that it's an enhancement request, then they can change it, but it definitely looks like a bug to me. - Jonathan M Davis
Feb 21 2012
parent BLM <blm768 gmail.com> writes:
I've submitted it to the DMD developers. Hopefully it won't take too long to get
fixed; it looks like it would be a _fairly_ simple fix to make. (Since when is a
compiler fix simple?)
Feb 21 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-02-22 03:39, Jonathan M Davis wrote:
 On Wednesday, February 22, 2012 02:21:43 BLM wrote:
 I'm working on a project where I'm using overloaded virtual methods, and
 I've run into a challenge with overload sets.

 My code looks something like this:

 class Base {
 void get(ubyte b) {};
 }

 class Derived: Base {
 //alias Base.get get;
 void get(string s) {};
 }

 I've tried using an alias declaration to combine the two classes' overload
 sets in the derived class, but DMD complains that the alias conflicts with
 get(string). Is there some reasonably elegant way around this, or is it a
 limitation of the language?
Hmm. That's how you're _supposed_ to do it. Sounds like a bug. http://dlang.org/function.html It may be related to the fact that you don't have override on the derived class's get though. You're really supposed to, but it hasn't been promoted to an error yet (it's enabled with -w until it is). Try that and see if it fixes it. - Jonathan M Davis
He is overloading, not overriding. You have to start notice the difference when reading these posts :) -- /Jacob Carlborg
Feb 21 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, February 22, 2012 08:19:09 Jacob Carlborg wrote:
 He is overloading, not overriding. You have to start notice the
 difference when reading these posts :)
Well, it's both. He's overriding a base class function with a different signature. So, depending on how the compiler treats that, it could be considered either both an override and an overload or just an overload. And apparently the override attribute only applies to exact overloads. - Jonathan M Davis
Feb 21 2012
parent reply BLM <blm768 gmail.com> writes:
After messing around for a while, I figured out what is making DMD choke on my
file. The methods were defined in the base class using template mixins, and
apparently DMD doesn't like it when mixins, inheritance, overloading, and
aliases
are all in the same piece of code :)
Feb 22 2012
next sibling parent James Miller <james aatch.net> writes:
On 23 February 2012 13:15, BLM <blm768 gmail.com> wrote:
 After messing around for a while, I figured out what is making DMD choke on my
 file. The methods were defined in the base class using template mixins, and
 apparently DMD doesn't like it when mixins, inheritance, overloading, and
aliases
 are all in the same piece of code :)
And the award for "Most Meta Code" goes too... -- James Miller
Feb 23 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 24, 2012 at 04:06:52AM +1300, James Miller wrote:
 On 23 February 2012 13:15, BLM <blm768 gmail.com> wrote:
 After messing around for a while, I figured out what is making DMD choke on my
 file. The methods were defined in the base class using template mixins, and
 apparently DMD doesn't like it when mixins, inheritance, overloading, and
aliases
 are all in the same piece of code :)
And the award for "Most Meta Code" goes too...
[...] I dunno, but "most meta" to me implies recursive mixins... T -- For every argument for something, there is always an equal and opposite argument against it. Debates don't give answers, only wounded or inflated egos.
Feb 23 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 24 February 2012 12:06, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:
 On Fri, Feb 24, 2012 at 04:06:52AM +1300, James Miller wrote:
 On 23 February 2012 13:15, BLM <blm768 gmail.com> wrote:
 After messing around for a while, I figured out what is making DMD choke on my
 file. The methods were defined in the base class using template mixins, and
 apparently DMD doesn't like it when mixins, inheritance, overloading, and
aliases
 are all in the same piece of code :)
And the award for "Most Meta Code" goes too...
[...] I dunno, but "most meta" to me implies recursive mixins... T -- For every argument for something, there is always an equal and opposite argument against it. Debates don't give answers, only wounded or inflated egos.
True, I guess the "Most Meta Code" would be something that combines recursive mixins, string mixins, inheritance, overriding, overloading, aliases and is all inside a recursive template. Bonus points if the final result is a quine. -- James Miller
Feb 23 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 24, 2012 at 04:24:03PM +1300, James Miller wrote:
 On 24 February 2012 12:06, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:
 On Fri, Feb 24, 2012 at 04:06:52AM +1300, James Miller wrote:
 On 23 February 2012 13:15, BLM <blm768 gmail.com> wrote:
 After messing around for a while, I figured out what is making
 DMD choke on my file. The methods were defined in the base class
 using template mixins, and apparently DMD doesn't like it when
 mixins, inheritance, overloading, and aliases are all in the same
 piece of code :)
And the award for "Most Meta Code" goes too...
[...] I dunno, but "most meta" to me implies recursive mixins...
[.[..]
 True, I guess the "Most Meta Code" would be something that combines
 recursive mixins, string mixins, inheritance, overriding, overloading,
 aliases and is all inside a recursive template. Bonus points if the
 final result is a quine.
[...] You forgot reflection. The quine would generate itself by reflecting upon itself and reproducing itself via aliasing recursive mixins (by which I mean string mixins that contain "mixin" in the string, recursively) into a recursive template that generates a class hierarchy. Bonus points if the code *in theory* would reproduce itself, but actually causes dmd to run into an infinite loop. :P T -- If it breaks, you get to keep both pieces. -- Software disclaimer notice
Feb 24 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/21/2012 06:21 PM, BLM wrote:
 I'm working on a project where I'm using overloaded virtual methods, and I've
 run into a challenge with overload sets.

 My code looks something like this:

 class Base {
   void get(ubyte b) {};
 }

 class Derived: Base {
   //alias Base.get get;
   void get(string s) {};
 }

 I've tried using an alias declaration to combine the two classes' overload
 sets in the derived class, but DMD complains that the alias conflicts with
 get(string). Is there some reasonably elegant way around this, or is it a
 limitation of the language?
Your code actually works with dmd 2.058: import std.stdio; class Base { void get(ubyte b) { writeln("get(ubyte)"); } } class Derived: Base { alias Base.get get; void get(string s) { writeln("get(string)"); } } void main() { ubyte b; string s; auto o = new Derived(); o.get(b); o.get(s); } Outputs: get(ubyte) get(string) I have also corrected the indentation ;) and two extraneous semicolons. Ali
Feb 21 2012
parent BLM <blm768 gmail.com> writes:
Hmm... I guess I'll have to figure out why my code is behaving differently and
put
a test case together. Strange...

Thanks for fixing the semicolons. Old C++ habits die hard, even if one has
written
very little C++ :)
Feb 21 2012