www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - void myFunc(T: !=int)(T value)

reply tsalm <tsalm free.fr> writes:
Hello,

I want to do something like this :

class MyClass
{
   void myFunc(int intValue)
   {
     /* Do something with this int */
   }

   void myFunc(T: !=3Dint)(T valueNotInt)
   {
     /* valueNotInt can not be an int */
   }
}

Thanks in advance,
TSalm
Aug 22 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
-------------------------------------------
"tsalm" <tsalm free.fr> wrote in message 
news:op.ugacfwj7010shu papillon.lan...
Hello,

I want to do something like this :

class MyClass
{
   void myFunc(int intValue)
   {
     /* Do something with this int */
   }

   void myFunc(T: !=int)(T valueNotInt)
   {
     /* valueNotInt can not be an int */
   }
}

Thanks in advance,
TSalm
-------------------------------------------

void myFunc(T: int)(int intValue) {}
void myFunc(T)(T notIntValue) {}

:) 
Aug 22 2008
parent reply tsalm <tsalm free.fr> writes:
Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley  
<kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(
Aug 22 2008
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"tsalm" wrote
 Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley 
 <kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(

Ah, your problem is that template methods cannot be virtual. .sort is trying to use the virtual method int opCmp(Object) in Object. Overload with Object as the argument for the non-int function: int opCmp(Object o); // this will be used in .sort int opCmp(int i); -Steve
Aug 22 2008
next sibling parent reply tsalm <tsalm free.fr> writes:
Le Fri, 22 Aug 2008 15:23:11 +0200, Steven Schveighoffer  
<schveiguy yahoo.com> a écrit:

 "tsalm" wrote
 Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley
 <kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(

Ah, your problem is that template methods cannot be virtual. .sort is trying to use the virtual method int opCmp(Object) in Object. Overload with Object as the argument for the non-int function: int opCmp(Object o); // this will be used in .sort int opCmp(int i); -Steve

I don't explain myself clearly ... This problem concern a "bug" on tango : http://www.dsource.org/projects/tango/ticket/1259#preview Here's a better example : /***************************************/ struct MyStruct { int a; // Must be use to sort this struct on an array // but the compilation send a conflict with "opCmp(T)(T val)" int opCmp(MyStruct m) { return a - m.a; } // I need this to compare with other objects int opCmp(T)(T val) { /* ... */} } void main() { MyStruct a,b,c,d; a.a = 5; b.a = 1; c.a = 10; d.a = -1; MyStruct[] array = [a,b,c,d]; array.sort; foreach(el;array) { Stdout(el.a).newline; } } /***************************************/
Aug 22 2008
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"tsalm" wrote
 Le Fri, 22 Aug 2008 15:23:11 +0200, Steven Schveighoffer 
 <schveiguy yahoo.com> a écrit:

 "tsalm" wrote
 Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley
 <kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(

Ah, your problem is that template methods cannot be virtual. .sort is trying to use the virtual method int opCmp(Object) in Object. Overload with Object as the argument for the non-int function: int opCmp(Object o); // this will be used in .sort int opCmp(int i); -Steve

I don't explain myself clearly ... This problem concern a "bug" on tango : http://www.dsource.org/projects/tango/ticket/1259#preview Here's a better example : /***************************************/ struct MyStruct { int a; // Must be use to sort this struct on an array // but the compilation send a conflict with "opCmp(T)(T val)" int opCmp(MyStruct m) { return a - m.a; } // I need this to compare with other objects int opCmp(T)(T val) { /* ... */} } void main() { MyStruct a,b,c,d; a.a = 5; b.a = 1; c.a = 10; d.a = -1; MyStruct[] array = [a,b,c,d]; array.sort; foreach(el;array) { Stdout(el.a).newline; } } /***************************************/

OK, I see what you mean. The compiler has some special knowledge of opCmp for structs (and toString, etc.) What it does is if you define int opCmp(MyStruct x), the compiler flags it as being the compare function for the struct, and the function pointer for opCmp goes into the TypeInfo for the struct as xopCmp or something like that. Then generic code can use the xopCmp sort of like a vtable pointer to compare two instances. Apparently, if that opCmp is defined by a template, it doesn't store that version in the xopCmp function pointer. As far as not compiling the above code, that is a limitation that is unlikely to change (overloading a member function with a template). -Steve
Aug 22 2008
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
tsalm wrote:
 Le Fri, 22 Aug 2008 15:23:11 +0200, Steven Schveighoffer 
 <schveiguy yahoo.com> a écrit:
 
 "tsalm" wrote
 Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley
 <kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(

Ah, your problem is that template methods cannot be virtual. .sort is trying to use the virtual method int opCmp(Object) in Object. Overload with Object as the argument for the non-int function: int opCmp(Object o); // this will be used in .sort int opCmp(int i); -Steve

I don't explain myself clearly ... This problem concern a "bug" on tango : http://www.dsource.org/projects/tango/ticket/1259#preview Here's a better example : /***************************************/ struct MyStruct { int a; // Must be use to sort this struct on an array // but the compilation send a conflict with "opCmp(T)(T val)"

You aren't currently allowed to overload template and non-template functions. It's an annoying limitation that is either already fixed or will be fixed in D 2.0. Sean
Aug 22 2008
parent reply tsalm <tsalm free.fr> writes:
 You aren't currently allowed to overload template and non-template  
 functions.  It's an annoying limitation that is either already fixed or  
 will be fixed in D 2.0.

thanks
Aug 22 2008
parent reply Sean Kelly <sean invisibleduck.org> writes:
For the record, though, this should work:

struct S
{
    int opCmp()( S val ) {}
    int opCmp(T)( T val ) {}
}

ie. you make both templates and one just has no parameters.  I'm not
sure if the compiler will be able to make this work with the built-in
sort routine, however, so you may want to try:

struct S
{
    int opCmp(T)( T val ) {}
    alias opCmp!(S) opCmp;
}

Or something like that as well.  I'd like to believe that there is some way
to make this work with the compiler as-is.


Sean
Aug 22 2008
parent tsalm <tsalm free.fr> writes:
Le Fri, 22 Aug 2008 23:31:18 +0200, Sean Kelly <sean invisibleduck.org> a  
écrit:

 For the record, though, this should work:

 struct S
 {
     int opCmp()( S val ) {}
     int opCmp(T)( T val ) {}
 }

 ie. you make both templates and one just has no parameters.  I'm not
 sure if the compiler will be able to make this work with the built-in
 sort routine, however, so you may want to try:

 struct S
 {
     int opCmp(T)( T val ) {}
     alias opCmp!(S) opCmp;
 }

 Or something like that as well.  I'd like to believe that there is some  
 way
 to make this work with the compiler as-is.

No, none of the twice :( struct S { int opCmp()( S val ) {} int opCmp(T)( T val ) {} } return opCmp() matches more than one function template declaration, opCmp() and opCmp(T) and the alias : alias sortBug.A.opCmp recursive alias declaration But I'm on dmd 1.0, maybe it's resolv on 2.0 ...
Aug 23 2008
prev sibling parent tsalm <tsalm free.fr> writes:
Le Fri, 22 Aug 2008 15:23:11 +0200, Steven Schveighoffer  
<schveiguy yahoo.com> a écrit:

 "tsalm" wrote
 Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley
 <kb3ctd2 yahoo.com> a écrit:

 -------------------------------------------
 "tsalm" <tsalm free.fr> wrote in message
 news:op.ugacfwj7010shu papillon.lan...
 Hello,

 I want to do something like this :

 class MyClass
 {
    void myFunc(int intValue)
    {
      /* Do something with this int */
    }

    void myFunc(T: !=int)(T valueNotInt)
    {
      /* valueNotInt can not be an int */
    }
 }

 Thanks in advance,
 TSalm
 -------------------------------------------

 void myFunc(T: int)(int intValue) {}
 void myFunc(T)(T notIntValue) {}

 :)

In fact, the exact operator is : int opCmp(MyStruct m) It must be use, when this struct is in an array, to sort this array And strangly, the method int opCmp(T:MyStruct)(MyStruct m) is not take by ".sort" :(

Ah, your problem is that template methods cannot be virtual. .sort is trying to use the virtual method int opCmp(Object) in Object. Overload with Object as the argument for the non-int function: int opCmp(Object o); // this will be used in .sort int opCmp(int i); -Steve

I don't explain myself clearly ... This problem concern a "bug" on tango : http://www.dsource.org/projects/tango/ticket/1259#preview Here's a better example : /***************************************/ struct MyStruct { int a; // Must be use to sort this struct on an array // but the compilation send a conflict with "opCmp(T)(T val)" int opCmp(MyStruct m) { return a - m.a; } // I need this to compare with other objects int opCmp(T)(T val) { /* ... */} } void main() { MyStruct a,b,c,d; a.a = 5; b.a = 1; c.a = 10; d.a = -1; MyStruct[] array = [a,b,c,d]; array.sort; foreach(el;array) { Stdout(el.a).newline; } } /***************************************/
Aug 22 2008