www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Should this Compile?

reply SamwiseFilmore <mggmugginsmc gmail.com> writes:
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
next sibling parent reply SamwiseFilmore <mggmugginsmc gmail.com> writes:
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
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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:
 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.
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 Davis
Oct 03 2017
parent SamwiseFilmore <mggmugginsmc gmail.com> writes:
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:
 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.
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 Davis
Thanks, that clears things up. I appreciate it!
Oct 03 2017
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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