Should this Compile?
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Oct 3 22:41:19 UTC 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."
More information about the Digitalmars-d-learn
mailing list