digitalmars.D.learn - Should this Compile?
- SamwiseFilmore (25/25) Oct 03 2017 I've created toString() for a struct (which is a lot more
- SamwiseFilmore (3/5) Oct 03 2017 I wrapped the ternary in parentheses, and it compiled. Still, I'm
- Jonathan M Davis (13/18) Oct 03 2017 Operator precedence makes it quite clear how the ternary operator is
- SamwiseFilmore (3/17) Oct 03 2017 Thanks, that clears things up. I appreciate it!
- H. S. Teoh (16/31) Oct 03 2017 [...]
I've created toString() for a struct (which is a lot more complicated than what I've provided here) that returns a large number of concatenated strings. Here is the example: struct Card { // Flesh out these enums appropriately CardSuit suit; CardValue value; Facing facing; string toString() { return this.value.toString() ~ " of " ~ this.suit.toString() ~ this.suit == CardSuit.diamonds ? "" : "\t" ~ "\tfacing " ~ this.facing.toString(); } } This code does not compile with this error message: Error: incompatible types for ((toString(this.p_value) ~ " of " ~ toString(this.p_suit)) ~ (this.p_suit)): 'string' and 'CardSuit' Am I using the ternary operator correctly here, or is this an issue with dmd? I'm using dmd v2.076.0. This is the ideal way to implement this function, although I could do it other ways.
Oct 03 2017
On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore wrote:Am I using the ternary operator correctly here, or is this an issue with dmd? I'm using dmd v2.076.0.I wrapped the ternary in parentheses, and it compiled. Still, I'm wondering about this behavior.
Oct 03 2017
On Tuesday, October 03, 2017 22:42:35 SamwiseFilmore via Digitalmars-d-learn wrote:On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore wrote:Operator precedence makes it quite clear how the ternary operator is supposed to function, and if you're ever not sure, then put parens around it. Almost every operator has higher precedence than the ternary operator, so aside from simple assignments, you typically have to use parens around it. For some reason, a lot of folks seem to assume that the ternary operator has much higher precedence than it does and get confused by the result, but if you just look at the operator precedence table, it should be pretty obvious why you're seeing what you're seeing: https://wiki.dlang.org/Operator_precedence - Jonathan M DavisAm I using the ternary operator correctly here, or is this an issue with dmd? I'm using dmd v2.076.0.I wrapped the ternary in parentheses, and it compiled. Still, I'm wondering about this behavior.
Oct 03 2017
On Tuesday, 3 October 2017 at 23:13:00 UTC, Jonathan M Davis wrote:On Tuesday, October 03, 2017 22:42:35 SamwiseFilmore via Digitalmars-d-learn wrote:Thanks, that clears things up. I appreciate it!On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore wrote:Operator precedence makes it quite clear how the ternary operator is supposed to function, and if you're ever not sure, then put parens around it. https://wiki.dlang.org/Operator_precedence - Jonathan M DavisAm I using the ternary operator correctly here, or is this an issue with dmd? I'm using dmd v2.076.0.I wrapped the ternary in parentheses, and it compiled. Still, I'm wondering about this behavior.
Oct 03 2017
On Tue, Oct 03, 2017 at 10:37:17PM +0000, SamwiseFilmore via Digitalmars-d-learn wrote: [...]string toString() { return this.value.toString() ~ " of " ~ this.suit.toString() ~ this.suit == CardSuit.diamonds ? "" : "\t" ~ "\tfacing " ~ this.facing.toString(); }[...]This code does not compile with this error message: Error: incompatible types for ((toString(this.p_value) ~ " of " ~ toString(this.p_suit)) ~ (this.p_suit)): 'string' and 'CardSuit' Am I using the ternary operator correctly here, or is this an issue with dmd? I'm using dmd v2.076.0.When in doubt, always parenthesize around the ?: operator to prevent ambiguities. I'd write that line as: ... ~ ((this.suit == CardSuit.diamonds) ? "" : "\t") ~ ... It's a few characters more, but will save you headaches from obscure errors caused by unexpected operator precedences. T -- A linguistics professor was lecturing to his class one day. "In English," he said, "A double negative forms a positive. In some languages, though, such as Russian, a double negative is still a negative. However, there is no language wherein a double positive can form a negative." A voice from the back of the room piped up, "Yeah, yeah."
Oct 03 2017