Operator overloading of native types?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Dec 13 15:19:22 PST 2012


I'd like to overload the '*' operator to work with string arguments. Is
it possible? I tried the following, but apparently operator overloading
doesn't work at the package level?

	string opBinary(string op)(string repeatMe, int thisManyTimes)
		if (op=="*")
	{
		auto app = appender!string();
		while (thisManyTimes > 0) {
			app.put(repeatMe);
			thisManyTimes--;
		}
		return app.data;
	}

	void main() {
		writeln("spam" * 3);	// compile error
	}

Or is this just a very bad idea? ;-)


T

-- 
Старый друг лучше новых двух.


More information about the Digitalmars-d-learn mailing list