digitalmars.D.bugs - problem with overloading runtime functions?
- Jarrett Billingsley (26/26) Jul 12 2004 i ran into this when trying to make a toString() for a custom type.
- Walter (9/35) Jul 12 2004 Overloading of functions occur on a per-module basis. Functions in one
- Jarrett Billingsley (1/1) Jul 12 2004 ahh :) thanks.
- Jarrett Billingsley (12/15) Jul 12 2004 unfortunately, that gives me an error :S or rather, the corrected versi...
- Walter (4/12) Jul 12 2004 ack! I got it backwards:
- Jarrett Billingsley (5/7) Jul 13 2004 from my last post:
- Walter (26/28) Jul 13 2004 The following compiles & produces the expected output:
- Jarrett Billingsley (20/21) Jul 14 2004 ahh, but what if my toString() is in another module?
- J C Calvarese (29/61) Jul 25 2004 D's pretty flexible, but you have to know how to talk to it. I think
- Jarrett Billingsley (1/1) Jul 25 2004 rock on :) thank you much!
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
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 triedusingthe global scope ( .toString(x.x) ) but to no avail. it happens if float3 is a class or a struct as well.
Jul 12 2004
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
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message news:ccvmp4$1924$1 digitaldaemon.com...ack! I got it backwards: alias std.string.toString toString;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;
Jul 12 2004
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
"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(). :PThe 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
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
Jarrett Billingsley wrote: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 0char[] 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.-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 25 2004