www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum overloading

reply strtr <strtr spam.com> writes:
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
parent reply Robert Clipsham <robert octarineparrot.com> writes:
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
parent reply strtr <strtr spam.com> writes:
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
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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
parent reply strtr <strtr spam.com> writes:
== 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 22 2010
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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 22 2010
next sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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
parent strtr <strtr spam.com> writes:
== 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
prev sibling parent reply strtr <strtr spam.com> writes:
== 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
May 23 2010
next sibling parent strtr <strtr spam.com> writes:
----
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
prev sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.html
 (it strikes me that this is a necessary product of a loose type system)
It 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"; } I tried telling walter that enums don't and won't suffer from this problem.
May 23 2010
next sibling parent strtr <strtr spam.com> writes:
== Quote from Ellery Newcomer (ellery-newcomer utulsa.edu)'s article
 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.html
That's not really the D1 spec.. a bug report it is :D
 (it strikes me that this is a necessary product of a loose type system)
It 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"; }
Yep, 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)
 I tried telling walter that enums don't and won't suffer from this problem.
May 23 2010
prev sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 05/23/2010 02:05 PM, Simen kjaeraas wrote:
 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.
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 enums
May 23 2010