digitalmars.D.learn - enum overloading
- strtr (3/3) May 22 2010 I wanted to overload toString for my enums.
- Robert Clipsham (9/12) May 22 2010 ----
- strtr (15/15) May 22 2010 Sorry, should have included this :)
- Ary Borenszweig (4/23) May 22 2010 That's not overloading, you are expecting an implicit conversion from
- Ary Borenszweig (4/30) May 22 2010 Overloading is defining many functions with the same name but different
- strtr (3/33) May 22 2010 Well, actually, I was expecting std.string.toString(int) to be called fo...
- Ellery Newcomer (8/41) May 22 2010 That would work except
- Ellery Newcomer (2/3) May 22 2010 Strike this line, not sure what I was thinking of here
- strtr (2/6) May 23 2010 Saves me a bit of parsing allocation ;)
- strtr (6/13) May 23 2010 Nice, that seems to work.
- strtr (25/25) May 23 2010 ----
- Ellery Newcomer (13/16) May 23 2010 yes.
- strtr (4/21) May 23 2010 Yep, tricky one. I wouldn't be mad at the compiler to use the ninja one
- Simen kjaeraas (5/7) May 23 2010 True. However, treating enums as special for this means another
- Ellery Newcomer (4/9) May 23 2010 implying a very large portion of D is bad.
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
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
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
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
Ary Borenszweig wrote:strtr wrote:Overloading is defining many functions with the same name but different type arguments. you are expecting an implicit conversion fromSorry, 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,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
== Quote from Ary Borenszweig (ary esperanto.org.ar)'s articleAry Borenszweig wrote:Well, actually, I was expecting std.string.toString(int) to be called for the int and my toString(ENUM) for the enum..?strtr wrote:Overloading is defining many functions with the same name but different type arguments. you are expecting an implicit conversion fromSorry, 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,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
On 05/22/2010 05:08 PM, strtr wrote:== Quote from Ary Borenszweig (ary esperanto.org.ar)'s articleThat 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 templateAry Borenszweig wrote:Well, actually, I was expecting std.string.toString(int) to be called for the int and my toString(ENUM) for the enum..?strtr wrote:Overloading is defining many functions with the same name but different type arguments. you are expecting an implicit conversion fromSorry, 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,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
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 22 2010
== Quote from Ellery Newcomer (ellery-newcomer utulsa.edu)'s articleOn 05/22/2010 08:20 PM, Ellery Newcomer wrote:Saves me a bit of parsing allocation ;)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
== Quote from Ellery Newcomer (ellery-newcomer utulsa.edu)'s articleThat 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
May 23 2010
---- module e2_def; import std.string; static enum ENUM_2 { D, E, F }; char[] toString(ENUM_2) { return "ENUM_2"; } -- module main; import std.string : toString; import std.stdio; import e2_def : ENUM_2, toString; enum ENUM { A,B } char[] toString(ENUM e_){return "ENUM";} void main (){ writefln( toString(3) ); writefln( toString(ENUM.A) ); writefln( toString(ENUM_2.D) ); } ---- 3 ENUM ENUM_2 Yep seems to work like that.
May 23 2010
On 05/23/2010 07:35 AM, strtr wrote:Did I miss it or should I add a bug report?http://www.digitalmars.com/d/2.0/hijack.htmlyes. here's an example which acts differently if you don't have it: module a; import b; import std.math; import std.stdio; string toString(double){ return "you want to use this function";} void main(){ writeln(toString(cos(1.0)); } module b; string toString(real){ return "covert ninja you don't know about"; } I tried telling walter that enums don't and won't suffer from this problem.(it strikes me that this is a necessary product of a loose type system)It is? :)
May 23 2010
== Quote from Ellery Newcomer (ellery-newcomer utulsa.edu)'s articleOn 05/23/2010 07:35 AM, strtr wrote:That's not really the D1 spec.. a bug report it is :DDid I miss it or should I add a bug report?http://www.digitalmars.com/d/2.0/hijack.htmlYep, tricky one. I wouldn't be mad at the compiler to use the ninja one as I do understand the reasoning (using D1 that is)yes. here's an example which acts differently if you don't have it: module a; import b; import std.math; import std.stdio; string toString(double){ return "you want to use this function";} void main(){ writeln(toString(cos(1.0)); } module b; string toString(real){ return "covert ninja you don't know about"; }(it strikes me that this is a necessary product of a loose type system)It is? :)I tried telling walter that enums don't and won't suffer from this problem.
May 23 2010
Ellery Newcomer <ellery-newcomer utulsa.edu> wrote:I tried telling walter that enums don't and won't suffer from this problem.True. However, treating enums as special for this means another special case in the language. And special cases are bad. -- Simen
May 23 2010
On 05/23/2010 02:05 PM, Simen kjaeraas wrote:Ellery Newcomer <ellery-newcomer utulsa.edu> wrote:implying a very large portion of D is bad. Seriously, there's all kinds of crap revolving around the basic types. Those are the special cases, not enumsI tried telling walter that enums don't and won't suffer from this problem.True. However, treating enums as special for this means another special case in the language. And special cases are bad.
May 23 2010