https://issues.dlang.org/show_bug.cgi?id=24842
Issue ID: 24842
Summary: No ability to overload unary logial not operator
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: [email protected]
Reporter: [email protected]
Dlang has wide range support for overloading nearly all operators the language
supports. However, I'm missing support for the unary logical not operator.
Usecases are implementation of (feature-)flags, or my current use-case,
notation of if an certain format is allowed or not for serialization /
processing. Essentially all datastuctures that are logically negateable would
benefit of the ability to overload the logical not operator.
Example:
```d
struct FormatId {
string id;
bool allowed = true;
this(string id) {
this.id = id;
}
private this(string id, bool allowed) {
this.id = id;
this.allowed = allowed;
}
auto opUnary(string op)() if (op == "!") {
return FormatId(this.id, !this.allowed);
}
}
unittest {
auto f1 = FormatId("test");
assert(f1.allowed == true);
auto f2 = !f1;
assert(f1.allowed == true);
assert(f2.allowed == false);
}
```
--