www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - unary operator overloading syntax

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench robertmuench.de> writes:
Hi, why does this work:

void* opUnary(string s)() if (s == "*"){...

but this not:

void* opUnary("*")() {...


If the first syntax is the only possible one, can I have several 
opUnary entries per struct/class or do I have to build a big switch 
inside the code section and use "s" to dispatch to correct handler?

-- 
Robert M. Münch
http://www.robertmuench.de
Jun 12 2010
next sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
Robert M. Münch wrote:

 Hi, why does this work:
 
 void* opUnary(string s)() if (s == "*"){...
 
 but this not:
 
 void* opUnary("*")() {...
 
 
 If the first syntax is the only possible one, can I have several
 opUnary entries per struct/class or do I have to build a big switch
 inside the code section and use "s" to dispatch to correct handler?
 
(string s) is a (template) parameter to opUnary, containing the operator to be matched, so only the latter is valid syntax. You can have multiple opUnary templates distinguished by constraints, or a switch, or a combination thereof.
Jun 12 2010
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench robertmuench.de> writes:
On 2010-06-12 12:20:14 +0200, Lutger said:

 (string s) is a (template) parameter to opUnary, containing the 
 operator to be matched, so only the latter is valid syntax.
You mean, "only the first syntax is valid" because the second version (latter) doesn't compile for me. -- Robert M. Münch http://www.robertmuench.de
Jun 12 2010
parent Lutger <lutger.blijdestijn gmail.com> writes:
Robert M. Münch wrote:

 On 2010-06-12 12:20:14 +0200, Lutger said:
 
 (string s) is a (template) parameter to opUnary, containing the
 operator to be matched, so only the latter is valid syntax.
You mean, "only the first syntax is valid" because the second version (latter) doesn't compile for me.
Yes, sorry, that was a mistake. Note you can also do things like this with constraints: void* opUnary(string s)() if (s == "*") { ... } void* opUnary(string s)() if (s=="++" || s=="--" || s=="+" || s=="-") { ... }
Jun 12 2010
prev sibling parent Trass3r <un known.com> writes:
 void* opUnary("*")() {...
Gotta be opUnary(string op : "*")() {
Jun 13 2010