www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - problem with overloading runtime functions?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
i ran into this when trying to make a toString() for a custom type.

these two functions compile fine:

char[] a(int x)
{
 return "hi";
}

struct float3
{
    float x,y,z;
}

char[] a(float3 x)
{
 a(cast(int)x.x);
 return "bye";
}



but this one whines:

char[] toString(float3 x)
{
 toString(x.x);
 return "bye";
}

it says basically that the first line (toString(x.x)) does not fit the
parameters of (float3).  it thinks that i want to call toString()
recursively, even though there is a toString(float) defined!  i tried using
the global scope ( .toString(x.x) ) but to no avail.

it happens if float3 is a class or a struct as well.
Jul 12 2004
parent reply "Walter" <newshound digitalmars.com> writes:
Overloading of functions occur on a per-module basis. Functions in one
module don't overload with functions in another module. To do that, you'll
need to add an alias, for example:

import string;
alias toString string.toString;

char[] toString(float3 x) {...}

"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
news:ccvco3$osi$1 digitaldaemon.com...
 i ran into this when trying to make a toString() for a custom type.

 these two functions compile fine:

 char[] a(int x)
 {
  return "hi";
 }

 struct float3
 {
     float x,y,z;
 }

 char[] a(float3 x)
 {
  a(cast(int)x.x);
  return "bye";
 }



 but this one whines:

 char[] toString(float3 x)
 {
  toString(x.x);
  return "bye";
 }

 it says basically that the first line (toString(x.x)) does not fit the
 parameters of (float3).  it thinks that i want to call toString()
 recursively, even though there is a toString(float) defined!  i tried

 the global scope ( .toString(x.x) ) but to no avail.

 it happens if float3 is a class or a struct as well.

Jul 12 2004
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
ahh :)  thanks.
Jul 12 2004
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 import string;
 alias toString string.toString;

 char[] toString(float3 x) {...}

unfortunately, that gives me an error :S or rather, the corrected version does. private import std.string; alias toString std.string.toString; it says that that's not a valid alias. (or rather, it gives me a few weird errors that basically sum up to mean that the alias is invalid). i tried switching the identifiers but that didn't help. i did manage to get the function to work, by explicitly qualifying the toStrings i.e. std.string.toString(x.x). however, this has the side effect that if i want to use MY toString, i then also have to explicitly qualify mine, as in mymodule.toString(). irritating!
Jul 12 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
news:ccvmp4$1924$1 digitaldaemon.com...
 import string;
 alias toString string.toString;

 char[] toString(float3 x) {...}

unfortunately, that gives me an error :S or rather, the corrected version does. private import std.string; alias toString std.string.toString;

ack! I got it backwards: alias std.string.toString toString;
Jul 12 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 ack! I got it backwards:

     alias std.string.toString toString;

from my last post: "i tried switching the identifiers but that didn't help." ;) i still can't get it to work correctly. i'm still stuck writing mymodule.toString(). :P
Jul 13 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
news:cd0q3r$826$1 digitaldaemon.com...
 i still can't get it to work correctly.  i'm still stuck writing
 mymodule.toString().  :P

The following compiles & produces the expected output: ------------------------------------------------------------- C:\mars>type test.d import std.stdio; import std.string; alias std.string.toString toString; struct X { } char[] toString(X x) { return "mine"; } void main() { X x; char[] s; s = toString(x); writefln(s); s = toString(6); writefln(s); } C:\mars>dmd test \dm\bin\link test,,,user32+kernel32/noi; C:\mars>test mine 6 C:\cbx\mars> ------------------------------------------------------------
Jul 13 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 char[] toString(X x) { return "mine"; }

ahh, but what if my toString() is in another module? [mymodule.d] -------------------------------------- module mymodule; private import std.string; alias std.string.toString toString; struct X {} char[] toString(X x) { return "jasidj"; } [main.d]-------------------------------------------- import mymodule; import std.string; alias std.string.toString toString; void main() { X x; toString(x); // error, incorrect params mymod.toString(x); // works fine } ------------------------------------------------------ i've tried putting the alias in neither, one of the files, or both, and nothing works.
Jul 14 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Jarrett Billingsley wrote:
char[] toString(X x) { return "mine"; }

ahh, but what if my toString() is in another module?

D's pretty flexible, but you have to know how to talk to it. I think this is what you want (or it's pretty close): //----[mymodule.d]---- module mymodule; private import std.string; alias std.string.toString toString; struct X {} char[] toString(X x) { return "jasidj"; } //----[main.d]---- import mymodule; import std.stdio; import std.string; alias mymodule.toString toString; void main() { X x; toString(x); writef(toString(x) ~ \n); writef(toString(0) ~ \n); } ----[Compile like this]---- dmd main.d mymodule.d ----[Output]---- jasidj 0
 
 [mymodule.d] --------------------------------------
 module mymodule;
 
 private import std.string;
 alias std.string.toString toString;
 
 struct X {}
 
 char[] toString(X x) { return "jasidj"; }
 
 [main.d]--------------------------------------------
 import mymodule;
 import std.string;
 
 alias std.string.toString toString;
 
 void main()
 {
  X x;
  toString(x);                   // error, incorrect params
  mymod.toString(x);      // works fine
 }
 
 ------------------------------------------------------
 
 i've tried putting the alias in neither, one of the files, or both, and
 nothing works.

-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 25 2004
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
rock on :)  thank you much!
Jul 25 2004