Skip to content

Commit

Permalink
fix Issue 3988 - Provide canonical example for operator overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jan 24, 2012
1 parent 3f3a45c commit ed45694
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions operatoroverloading.dd
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,26 @@ b.opBinaryRight!("$(I op)")(a)
It is an error for both to equally match.
)

$(P Operator overloading for a number of operators can be done at the same time.
For example, if only the + or - operators are supported:)

---
T opBinary(string op)(T rhs) {
static if (op == "+") return data + rhs.data;
else static if (op == "-") return data - rhs.data;
else static assert(0, "Operator "~op~" not implemented");
}
---

$(P To do them all en masse:)

---
T opBinary(string op)(T rhs) {
return mixin("data "~op~" rhs.data");
}
---


<h2><a name="equals">Overloading == and !=</a></h2>

$(P Expressions of the form $(CODE a != b) are rewritten as $(CODE !(a == b)).)
Expand Down

0 comments on commit ed45694

Please sign in to comment.