Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 02, 2005 toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
The following code generates an error: ----- import std.string; class MyClass { void Display() { toString(1); // This line generates an error } } void main(char[][] args) { } ------ The compiler error is: test.d(7): function object.Object.toString () does not match argument types (int) test.d(7): Error: expected 0 arguments, not 1 The following variations compile fine: ----- import std.string; class MyClass { void Display() { std.string.toString(1); // Works fine } } void main(char[][] args) { } ----- void main(char[][] args) { toString(1); // Works fine } ----- |
February 02, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | This is with DMD 0.110. I haven't tried 0.112 yet. "Nick Sabalausky" <z@a.a> wrote in message news:ctpknf$21se$1@digitaldaemon.com... > The following code generates an error: > > ----- > import std.string; > class MyClass > { > void Display() > { > toString(1); // This line generates an error > } > } > void main(char[][] args) > { > } > ------ > > The compiler error is: > test.d(7): function object.Object.toString () does not match argument > types (int) > test.d(7): Error: expected 0 arguments, not 1 > > The following variations compile fine: > ----- > import std.string; > class MyClass > { > void Display() > { > std.string.toString(1); // Works fine > } > } > void main(char[][] args) > { > } > ----- > void main(char[][] args) > { > toString(1); // Works fine > } > ----- > > |
February 02, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | I just tried DMD 0.112 and got the same results. "Nick Sabalausky" <z@a.a> wrote in message news:ctpkqk$21ud$1@digitaldaemon.com... > This is with DMD 0.110. I haven't tried 0.112 yet. > > "Nick Sabalausky" <z@a.a> wrote in message news:ctpknf$21se$1@digitaldaemon.com... >> The following code generates an error: >> >> ----- >> import std.string; >> class MyClass >> { >> void Display() >> { >> toString(1); // This line generates an error >> } >> } >> void main(char[][] args) >> { >> } >> ------ >> >> The compiler error is: >> test.d(7): function object.Object.toString () does not match argument >> types (int) >> test.d(7): Error: expected 0 arguments, not 1 >> >> The following variations compile fine: >> ----- >> import std.string; >> class MyClass >> { >> void Display() >> { >> std.string.toString(1); // Works fine >> } >> } >> void main(char[][] args) >> { >> } >> ----- >> void main(char[][] args) >> { >> toString(1); // Works fine >> } >> ----- >> >> > > |
February 02, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | I don't think this is a bug, because MyClass derives from Object and
inherits the "char[] toString()" method which takes no argument.
You have to explicitly write std.string.toString here.
Nick Sabalausky wrote:
> The following code generates an error:
>
> -----
> import std.string;
> class MyClass
> {
> void Display()
> {
> toString(1); // This line generates an error
> }
> }
> void main(char[][] args)
> {
> }
> ------
>
> The compiler error is:
> test.d(7): function object.Object.toString () does not match argument types (int)
> test.d(7): Error: expected 0 arguments, not 1
>
> The following variations compile fine:
> -----
> import std.string;
> class MyClass
> {
> void Display()
> {
> std.string.toString(1); // Works fine
> }
> }
> void main(char[][] args)
> {
> }
> -----
> void main(char[][] args)
> {
> toString(1); // Works fine
> }
> -----
>
>
|
February 02, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | Ahh, I see. Now that you mention that, it's occurred to me that the module scope operator works too: class MyClass { void Display() { .toString(1); // That little dot does wonders ;) } } "zwang" <nehzgnaw@gmail.com> wrote in message news:ctpldp$22ba$1@digitaldaemon.com... >I don't think this is a bug, because MyClass derives from Object and > inherits the "char[] toString()" method which takes no argument. You have to explicitly write std.string.toString here. > > > Nick Sabalausky wrote: >> The following code generates an error: >> >> ----- >> import std.string; >> class MyClass >> { >> void Display() >> { >> toString(1); // This line generates an error >> } >> } >> void main(char[][] args) >> { >> } >> ------ >> >> The compiler error is: >> test.d(7): function object.Object.toString () does not match argument >> types (int) >> test.d(7): Error: expected 0 arguments, not 1 >> >> The following variations compile fine: >> ----- >> import std.string; >> class MyClass >> { >> void Display() >> { >> std.string.toString(1); // Works fine >> } >> } >> void main(char[][] args) >> { >> } >> ----- >> void main(char[][] args) >> { >> toString(1); // Works fine >> } >> ----- >> |
February 03, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky Attachments: | Added to DStress as http://dstress.kuehne.cn/run/overload_14.d http://dstress.kuehne.cn/run/overload_15.d http://dstress.kuehne.cn/run/overload_16.d Note: this behavior might actually be the desired one, but I couldn't find and documentation stating so. Thomas |
February 03, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | On Thu, 03 Feb 2005 05:01:47 +0100, Thomas Kuehne <thomas-dloop@kuehne.THISISSPAM.cn> wrote: > Added to DStress as > > http://dstress.kuehne.cn/run/overload_14.d > http://dstress.kuehne.cn/run/overload_15.d > http://dstress.kuehne.cn/run/overload_16.d > > Note: this behavior might actually be the desired one, but I couldn't > find and documentation stating so. I believe it is, my reasoning: The class inherits a toString method from it's parent "Object". When you type "toString(1);" in a class method, name resolution begins by looking for a symbol called toString in the current scope, i.e. the class, then it looks in the parent class, finds toString() and gives an error because the number of parameters is all wrong. The solution is to explicitly call the function you mean, or add an alias to the class eg. class MyClass { alias std.string.toString toString; void Display() { toString(1); // This line generates an error } } Regan |
February 03, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath Attachments: | Regan Heath wrote: | On Thu, 03 Feb 2005 05:01:47 +0100, Thomas Kuehne | <thomas-dloop@kuehne.THISISSPAM.cn> wrote: | |> Added to DStress as |> |> http://dstress.kuehne.cn/run/overload_14.d |> http://dstress.kuehne.cn/run/overload_15.d |> http://dstress.kuehne.cn/run/overload_16.d |> |> Note: this behavior might actually be the desired one, but I |> couldn't find and documentation stating so. | | | I believe it is, my reasoning: | | The class inherits a toString method from it's parent "Object". | | When you type "toString(1);" in a class method, name resolution | begins by looking for a symbol called toString in the current scope, | i.e. the class, then it looks in the parent class, finds toString() | and gives an error because the number of parameters is all wrong. The documentation is missing the statement about scoping-overloading interaction. The documentation only talks about overloading functions in base classes and not about overloading functions that are part of the class' parent scope(non-class member functions or class member functions of enclosing classes). Thomas |
March 16, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | "Thomas Kuehne" <thomas-dloop@kuehne.THISISSPAM.cn> wrote in message news:ium8d2-vq3.ln1@lnews.kuehne.cn... > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Regan Heath wrote: > | On Thu, 03 Feb 2005 05:01:47 +0100, Thomas Kuehne > | <thomas-dloop@kuehne.THISISSPAM.cn> wrote: > | > |> Added to DStress as > |> > |> http://dstress.kuehne.cn/run/overload_14.d > |> http://dstress.kuehne.cn/run/overload_15.d > |> http://dstress.kuehne.cn/run/overload_16.d > |> > |> Note: this behavior might actually be the desired one, but I > |> couldn't find and documentation stating so. > | > | > | I believe it is, my reasoning: > | > | The class inherits a toString method from it's parent "Object". > | > | When you type "toString(1);" in a class method, name resolution > | begins by looking for a symbol called toString in the current scope, > | i.e. the class, then it looks in the parent class, finds toString() > | and gives an error because the number of parameters is all wrong. > > The documentation is missing the statement about scoping-overloading interaction. > > The documentation only talks about overloading functions in base classes and not about overloading functions that are part of the class' parent scope(non-class member functions or class member functions of enclosing classes). It follows the rule that name resolution happens before overload resolution. Names outside the scope where the name was found do not participate in overload resolution. |
March 17, 2005 Re: toString() not working within class member function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Walter schrieb am Wed, 16 Mar 2005 13:39:13 -0800: > >> |> http://dstress.kuehne.cn/run/overload_14.d >> |> http://dstress.kuehne.cn/run/overload_16.d > > It follows the rule that name resolution happens before overload resolution. Names outside the scope where the name was found do not participate in overload resolution. Thanks - moved to http://dstress.kuehne.cn/nocompile/overload_14.d http://dstress.kuehne.cn/nocompile/overload_16.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCOZuO3w+/yD4P9tIRAmJ+AKCtfo4hD8YAMtimM1Ggvy1om6TFagCgwPEv lPSlLGI/f//XnaZhNDaqcXI= =eJs/ -----END PGP SIGNATURE----- |
Copyright © 1999-2021 by the D Language Foundation