www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Overloading doesn't work like described in "The D programming language"

reply Michael Kremser <mkspamx-usenet yahoo.de> writes:
Hi!

On pages 145 and 146 (§ 5.5.1) of "The D programming language" there is 
an example with overloading a function with uint, long, and a 
parameterized type. I tried to reproduce that using a similar example:

<code>
module main;

import std.stdio;

void overloadme(uint number)
{
	writeln("This is overloadme with uint.");
}

void overloadme(long number)
{
	writeln("This is overloadme with long.");
}

void overloadme(T)(T number)
{
	writeln("Generic overloadme called.");
}

int main(string[] argv)
{
	overloadme(25);
	overloadme("Bla");

	writeln("\nFinished");
	readln();
	return 0;
}
</code>

However, if I try to compile that code, the compiler yields an error in 
line 15:

Error: template main.overloadme(T) conflicts with function 
main.overloadme at main.d(5)

In the book it says that "non-generic functions are generally preferred 
to generic functions, even when the non-generic function need an 
implicit conversion". But in my case that doesn't work.

Can anyone explain me what's going on here? Is the example in the book 
wrong or did I misinterpret something?

Best regards

Michael
Dec 07 2011
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, December 07, 2011 22:35:04 Michael Kremser wrote:
 Hi!
 
 On pages 145 and 146 (§ 5.5.1) of "The D programming language" there is
 an example with overloading a function with uint, long, and a
 parameterized type. I tried to reproduce that using a similar example:
 
 <code>
 module main;
 
 import std.stdio;
 
 void overloadme(uint number)
 {
 writeln("This is overloadme with uint.");
 }
 
 void overloadme(long number)
 {
 writeln("This is overloadme with long.");
 }
 
 void overloadme(T)(T number)
 {
 writeln("Generic overloadme called.");
 }
 
 int main(string[] argv)
 {
 overloadme(25);
 overloadme("Bla");
 
 writeln("\nFinished");
 readln();
 return 0;
 }
 </code>
 
 However, if I try to compile that code, the compiler yields an error in
 line 15:
 
 Error: template main.overloadme(T) conflicts with function
 main.overloadme at main.d(5)
 
 In the book it says that "non-generic functions are generally preferred
 to generic functions, even when the non-generic function need an
 implicit conversion". But in my case that doesn't work.
 
 Can anyone explain me what's going on here? Is the example in the book
 wrong or did I misinterpret something?
Currently, you cannot overload templated functions with non-templated ones. It should be fixed at some point, but it hasn't been yet. There's a bug report on it: http://d.puremagic.com/issues/show_bug.cgi?id=2972 The workaround is to templatize the non-templated functions with an empty template parameter list. e.g. void overloadme(uint number) becomes void overloadme()(uint number) - Jonathan M Davis
Dec 07 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 07 Dec 2011 16:35:04 -0500, Michael Kremser  =

<mkspamx-usenet yahoo.de> wrote:

 Hi!

 On pages 145 and 146 (=C2=A7 5.5.1) of "The D programming language" th=
ere is =
 an example with overloading a function with uint, long, and a  =
 parameterized type. I tried to reproduce that using a similar example:=
 <code>
 module main;

 import std.stdio;

 void overloadme(uint number)
 {
 	writeln("This is overloadme with uint.");
 }

 void overloadme(long number)
 {
 	writeln("This is overloadme with long.");
 }

 void overloadme(T)(T number)
 {
 	writeln("Generic overloadme called.");
 }

 int main(string[] argv)
 {
 	overloadme(25);
 	overloadme("Bla");

 	writeln("\nFinished");
 	readln();
 	return 0;
 }
 </code>

 However, if I try to compile that code, the compiler yields an error i=
n =
 line 15:

 Error: template main.overloadme(T) conflicts with function  =
 main.overloadme at main.d(5)

 In the book it says that "non-generic functions are generally preferre=
d =
 to generic functions, even when the non-generic function need an  =
 implicit conversion". But in my case that doesn't work.

 Can anyone explain me what's going on here? Is the example in the book=
=
 wrong or did I misinterpret something?
The compiler has not implemented overloads with templates yet. Looks like there isn't a bug on it yet... -Steve
Dec 07 2011