Clay language

Steven Schveighoffer schveiguy at yahoo.com
Thu Dec 30 14:21:23 PST 2010


On Thu, 30 Dec 2010 17:03:05 -0500, so <so at so.do> wrote:

>> 	?? The only type in this list without a division operator is vector
>> all the others have it.
>
> Matrix matrix, matrix vector, vector matrix division also not defined,  
> there is one syntactic similarity but it is not division.
> Didn't give much of a thought to others since vector, matrix and scalar  
> operations takes already quite a bit space.
>
>> 	I'm sorry? What do you call the "generic case" here? All this list
>> shows is that each operator needs to be implemented individually
>> anyway. Andrei's point was exactly the reverse: he claims that most
>> operators can be implemented in groups which clearly isn't the case
>
> I don't agree, majority of the cases you duplicate almost all of the  
> code and just change the operator. That was the thing i meant with  
> "generic case".
> If i wasn't clear, say:
>
> vector opBinary(string op)(scalar s) if(op == "+" || op == "-" ....) {
> 	static if(op == "/")
> 		return general("*")(1/s); // particular case
> 	else
> 		return general(op)(s); // general case, just a one-liner mixin
> }
>
> vector opBinary(string op)(vector v) if(op == "+" || op == "-" ....) {
> 	return general(op)(v); // again, just a one-liner mixin
> }
>
> Same goes for matrix, particular case being the multiplication.

Actually, that doesn't work currently.  But I think this is a situation  
that can be fixed.

Essentially, you can't overload templates.  You have to do something like  
this instead:

vector opBinary(string op, T)(T s) if(is(T == scalar) && (op == "+" || op  
== "-" ....)) {
	static if(op == "/")
		return general("*")(1/s); // particular case
	else
		return general(op)(s); // general case, just a one-liner mixin
}

vector opBinary(string op, T)(T v) if(is(T == vector) && (op == "+" || op  
== "-" ....)) {
	return general(op)(v); // again, just a one-liner mixin
}

So, it makes things difficult in this regard too, but I really hope this  
can be solved.  It's already been stated in TDPL that templates will be  
able to overload with non-templates.  I think this means they should  
overload with templates also.

Note that these solutions may look simple and easy to you, but they look  
convoluted and messy to me ;)

-Steve


More information about the Digitalmars-d mailing list