digitalmars.D.learn - operator overload for enum
- Satoshi (29/29) Jun 10 2016 Hello,
- Timon Gehr (6/36) Jun 10 2016 It's an arbitrary limitation.
Hello,
why operator overloading is not working as a static methods
through the UFCS?
I need overload a << operator in this way:
enum PlatformID {
None,
Trinix,
Unix,
MacOS,
Windows
}
PlatformID toPlatformID(string platform) {
switch (platform.toLower()) {
case "trinix": return PlatformID.Trinix;
case "unix": return PlatformID.Unix;
case "macos": return PlatformID.MacOS;
case "windows": return PlatformID.Windows;
default: return PlatformID.None;
}
}
void opBinary(string op)(ref PlatformID plat, auto ref const
string str) if (op == "<<") {
plat = toPlatformID(str);
}
void thisIsTest() {
PlatformID id;
id << "x86_64"; // this is not working
id.opBinary!("<<")("test"); // but this works O.K.
}
Jun 10 2016
On 10.06.2016 13:02, Satoshi wrote:Hello, why operator overloading is not working as a static methods through the UFCS? ...It's an arbitrary limitation. https://issues.dlang.org/show_bug.cgi?id=8062 (The specification has been updated in the meantime, it now documents the limitation explicitly. https://dlang.org/spec/operatoroverloading.html)I need overload a << operator in this way: enum PlatformID { None, Trinix, Unix, MacOS, Windows } PlatformID toPlatformID(string platform) { switch (platform.toLower()) { case "trinix": return PlatformID.Trinix; case "unix": return PlatformID.Unix; case "macos": return PlatformID.MacOS; case "windows": return PlatformID.Windows; default: return PlatformID.None; } } void opBinary(string op)(ref PlatformID plat, auto ref const string str) if (op == "<<") { plat = toPlatformID(str); } void thisIsTest() { PlatformID id; id << "x86_64"; // this is not working id.opBinary!("<<")("test"); // but this works O.K. }Yuck. This should at the very least be opAssign instead.
Jun 10 2016








Timon Gehr <timon.gehr gmx.ch>