Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 22, 2010 enum overloading | ||||
---|---|---|---|---|
| ||||
I wanted to overload toString for my enums. test.d(189): Error: toString (ENUM) does not match parameter types (int) not possible? |
May 22, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On 22/05/10 18:46, strtr wrote:
> I wanted to overload toString for my enums.
>
> test.d(189): Error: toString (ENUM) does not match parameter types (int)
>
> not possible?
----
enum ENUM
{
a, b, c
}
void toString(ENUM) { }
----
It works here. Could you show an example of some code that isn't working?
|
May 22, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | Sorry, should have included this :) ---- module main; import std.string; import std.stdio; enum ENUM { A,B } char[] toString(ENUM e_){return "enum";} void main (){ writefln( toString(3) ); writefln( toString(ENUM.A) ); } -- main.d(10): Error: function main.toString (ENUM) does not match parameter types (int) main.d(10): Error: cannot implicitly convert expression (3) of type int to ENUM ---- |
May 22, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | strtr wrote:
> Sorry, should have included this :)
>
> ----
> module main;
> import std.string;
> import std.stdio;
>
> enum ENUM { A,B }
>
> char[] toString(ENUM e_){return "enum";}
>
> void main (){
> writefln( toString(3) );
> writefln( toString(ENUM.A) );
> }
> --
> main.d(10): Error: function main.toString (ENUM) does not match parameter types (int)
> main.d(10): Error: cannot implicitly convert expression (3) of type int to ENUM
> ----
That's not overloading, you are expecting an implicit conversion from int to ENUM. Maybe if you cast 3 to ENUM, but still... no ENUM value will be found for 3. What are you trying to do?
|
May 22, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig wrote: > strtr wrote: >> Sorry, should have included this :) >> >> ---- >> module main; >> import std.string; >> import std.stdio; >> >> enum ENUM { A,B } >> >> char[] toString(ENUM e_){return "enum";} >> >> void main (){ >> writefln( toString(3) ); >> writefln( toString(ENUM.A) ); >> } >> -- >> main.d(10): Error: function main.toString (ENUM) does not match parameter types (int) >> main.d(10): Error: cannot implicitly convert expression (3) of type int to ENUM >> ---- > > That's not overloading, Overloading is defining many functions with the same name but different type arguments. you are expecting an implicit conversion from > int to ENUM. Maybe if you cast 3 to ENUM, but still... no ENUM value will be found for 3. What are you trying to do? |
May 22, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | == Quote from Ary Borenszweig (ary@esperanto.org.ar)'s article
> Ary Borenszweig wrote:
> > strtr wrote:
> >> Sorry, should have included this :)
> >>
> >> ----
> >> module main;
> >> import std.string;
> >> import std.stdio;
> >>
> >> enum ENUM { A,B }
> >>
> >> char[] toString(ENUM e_){return "enum";}
> >>
> >> void main (){
> >> writefln( toString(3) );
> >> writefln( toString(ENUM.A) );
> >> }
> >> --
> >> main.d(10): Error: function main.toString (ENUM) does not match
> >> parameter types (int)
> >> main.d(10): Error: cannot implicitly convert expression (3) of type
> >> int to ENUM
> >> ----
> >
> > That's not overloading,
> Overloading is defining many functions with the same name but different
> type arguments.
> you are expecting an implicit conversion from
> > int to ENUM. Maybe if you cast 3 to ENUM, but still... no ENUM value will be found for 3. What are you trying to do?
Well, actually, I was expecting std.string.toString(int) to be called for the int
and my toString(ENUM) for the enum..?
|
May 23, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On 05/22/2010 05:08 PM, strtr wrote:
> == Quote from Ary Borenszweig (ary@esperanto.org.ar)'s article
>> Ary Borenszweig wrote:
>>> strtr wrote:
>>>> Sorry, should have included this :)
>>>>
>>>> ----
>>>> module main;
>>>> import std.string;
>>>> import std.stdio;
>>>>
>>>> enum ENUM { A,B }
>>>>
>>>> char[] toString(ENUM e_){return "enum";}
>>>>
>>>> void main (){
>>>> writefln( toString(3) );
>>>> writefln( toString(ENUM.A) );
>>>> }
>>>> --
>>>> main.d(10): Error: function main.toString (ENUM) does not match
>>>> parameter types (int)
>>>> main.d(10): Error: cannot implicitly convert expression (3) of type
>>>> int to ENUM
>>>> ----
>>>
>>> That's not overloading,
>> Overloading is defining many functions with the same name but different
>> type arguments.
>> you are expecting an implicit conversion from
>>> int to ENUM. Maybe if you cast 3 to ENUM, but still... no ENUM value
>>> will be found for 3. What are you trying to do?
>
> Well, actually, I was expecting std.string.toString(int) to be called for the int
> and my toString(ENUM) for the enum..?
That would work except
a) walter's hijacking fetish; if you want to overload a function with one imported from an external module, you'd have to do something like
import std.string: toString;
(it strikes me that this is a necessary product of a loose type system)
From a discussion with walter a while back, I gathered not possible.
b) std.string.toString isn't a function - it's a template
|
May 23, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On 05/22/2010 08:20 PM, Ellery Newcomer wrote:
>
> From a discussion with walter a while back, I gathered not possible.
Strike this line, not sure what I was thinking of here
|
May 23, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | == Quote from Ellery Newcomer (ellery-newcomer@utulsa.edu)'s article
> On 05/22/2010 08:20 PM, Ellery Newcomer wrote:
> >
> > From a discussion with walter a while back, I gathered not possible.
> Strike this line, not sure what I was thinking of here
Saves me a bit of parsing allocation ;)
|
May 23, 2010 Re: enum overloading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | == Quote from Ellery Newcomer (ellery-newcomer@utulsa.edu)'s article > That would work except > a) walter's hijacking fetish; if you want to overload a function with > one imported from an external module, you'd have to do something like > import std.string: toString; Nice, that seems to work. So to overload functions from other modules you need to selectively import them.. Can't find anything about that in the docs. Did I miss it or should I add a bug report? > (it strikes me that this is a necessary product of a loose type system) It is? :) > From a discussion with walter a while back, I gathered not possible. > b) std.string.toString isn't a function - it's a template |
Copyright © 1999-2021 by the D Language Foundation