www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function Overloading

reply Jeremy Cowgar <jeremy AT cowgar DOT coooom> <Jeremy_member pathlink.com> writes:
Here's my example:

import std.date;

char[] toString(d_time dt, char[] format) {return null}

void main() {
d_time d;
toString(d);
}

Obviously this isn't a working example (but compilable). Enough has been given
to see what I am speaking about. When I compile this program I get:

$ dmd test.d
test.d(7): function test.toString (long,char[]) does not match argument types
(long)
test.d(7): Error: expected 2 arguments, not 1

Why wouldn't D be able to see std.date.toString? which is defined as:

char[] toString(d_time) {...}

The D spec states: "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." That would make me think that toString(d_time,
char[]) doesn't match, let's see if something else does.

Any comments would be appriciated. BTW... On my site
(http://jeremy.cowgar.com/D.html) I released 2 items that I have built. Both
need more work, but the are an object oriented wrapper around SQLite and also
Template4D, a web based templating engine.

---
Jeremy Cowgar
http://jeremy.cowgar.com
Apr 11 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 11 Apr 2005 22:30:55 +0000 (UTC), Jeremy Cowgar <jeremy AT cowgar  
DOT coooom Jeremy_member pathlink.com> wrote:
 Here's my example:

 import std.date;

 char[] toString(d_time dt, char[] format) {return null}

 void main() {
 d_time d;
 toString(d);
 }

 Obviously this isn't a working example (but compilable). Enough has been  
 given
 to see what I am speaking about. When I compile this program I get:

 $ dmd test.d
 test.d(7): function test.toString (long,char[]) does not match argument  
 types
 (long)
 test.d(7): Error: expected 2 arguments, not 1

 Why wouldn't D be able to see std.date.toString? which is defined as:

 char[] toString(d_time) {...}

 The D spec states: "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."
Yep.
 That would make me think that toString(d_time,
 char[]) doesn't match, let's see if something else does.
The paragraph above does not mention this, but the very first step is to find a method *name* match, it only tries another scope if it finds no matching method name. Once it has a method name match it does what is described in the paragraph. So, in this case it does name matching first in the innermost scope, finds a method name match, attempts and exact parameter match, fails, attempts implicit conversions on parameters, fails, gives an error. To bring "std.date.toString" into the innermost scope and cause a method name match you can use alias: alias std.date.toString toString; Or you can change your toString call to a full reference: std.date.toString(d) This behaviour is the same as in C++ (without the many varied exceptions and rules) but different to Java (which will continune looking in other scopes for name matches) There has been a lot of discussion on this topic, do a google search eg. [site:digitalmars.com/d "name resolution"] and you should find them. It all boils down to Walter preferring the current method, as it: - avoids the possibility of some nasty resolution bugs. - requires you to be explicit about what you want. Others dislike it as the explicit nature can cause a lot of work, particularly if you want Java like behaviour. Regan
Apr 11 2005