www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Signature selection help.

reply Derek <derek psych.ward> writes:
In the code below, DMD keeps telling me ...

test.d(9): function test.foo overloads int(real x,uint cnt) and int(dchar
x,uint cnt) both match argument list for foo

So why can't it tell which function I'm trying to use, and what can I do to
let it know which one I want. BTW I want to call the 'dchar' version when a
character literal is supplied.

<code>
int foo(real x, uint cnt){ return 1;}
int foo(dchar x, uint cnt){ return 2;}

void main()
{
    int x;
    
x = foo(cast(dchar)'a', 6);   //#1 - fails
// x = foo('a', 6); // #2 - fails
//x = foo(cast(real)'a', 6);  // #3 - compiles but uses wrong function.
}
</code>
-- 
Derek
Melbourne, Australia
Feb 19 2005
parent reply =?ISO-8859-1?Q?Thomas_K=FChne?= <thomas-dloop kuehne.THISISSPAM.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Derek wrote:
| In the code below, DMD keeps telling me ...
|
| test.d(9): function test.foo overloads int(real x,uint cnt) and
| int(dchar x,uint cnt) both match argument list for foo
|
| So why can't it tell which function I'm trying to use, and what can I
| fo to let it know which one I want. BTW I want to call the 'dchar'
| version when a character literal is supplied.
|
| <code>
| int foo(real x, uint cnt){ return 1;}
| int foo(dchar x, uint cnt){ return 2;}
|
| void main()
| {
|     int x;
|
| x = foo(cast(dchar)'a', 6);   //#1 - fails
	x = foo(cast(dchar)'a', 6u); // x=2
| // x = foo('a', 6); // #2 - fails
	x = foo('a', 6u);
	// expected failure
	// replace 'a'(char) with '\U00000061'(dchar) and it'll ork
| //x = foo(cast(real)'a', 6);  // #3 - compiles but uses wrong
| function.
	x = foo(cast(real)'a', 6u); // x=1
| }
| </code>

http://www.digitalmars.com/d/function.html
# In D, function overloading is simple. It matches exactly, it matches
# with implicit conversions, or it does not match. If there is more than
# one match, it is an error.

Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFCGFSy3w+/yD4P9tIRAtbFAJ48KwpQSqWcx5PPJWj5Naor0CpgvwCg0VfE
yg4wMCjg+SfmJWTjb8T+rvQ=
=hk4c
-----END PGP SIGNATURE-----
Feb 20 2005
parent Derek <derek psych.ward> writes:
On Sun, 20 Feb 2005 10:13:32 +0100, Thomas Kühne wrote:

Thanks Thomas, this has helped.

 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Derek wrote:
| In the code below, DMD keeps telling me ...
|
| test.d(9): function test.foo overloads int(real x,uint cnt) and
| int(dchar x,uint cnt) both match argument list for foo
|
| So why can't it tell which function I'm trying to use,

Okay, I know the matching rule... # In D, function overloading is simple. It matches exactly, it matches # with implicit conversions, or it does not match. If there is more than # one match, it is an error. It was the implicit conversion that threw me, and the actual text of the error message. If the error message had have read more like ... # test.d(9): function test.foo overloads int(real x,uint cnt) and # int(dchar x,uint cnt) both match argument list for foo(dchar,int) then I might have realized that it was the *second* parameter it was barfing over. I kept on thinking it was the first parameter.
| and what can I do to let it know which one I want.

I need to specify the exact storage type for literals in function calls, in order to avoid such things. -- Derek Melbourne, Australia
Feb 20 2005