Working my way through Ali Çehreli's rather amazing e-book, I've hit a snag where some code I've written is pretty crashy. I consistently get "Segmentation fault: 11" (dmd 2.067.1, OSX).

I can't figure out where things are going wrong, because any attempt I make to debug via extra print statements causes the program to run successfully. Same if I try to compile with "-gc" ... it suddenly starts working, so I can't debug with gdb.

Putting aside any "that's probably not a great solution to the problem you're tying to solve" thoughts, can anyone offer me the "eureka" moment I'm missing to understand why the code below doesn't work?

Many thanks in advance!

* * *

import std.stdio;

enum Suit {
        HEARTS, DIAMONDS, CLUBS, SPADES
}

enum Value {
ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}

struct Card {
        Value value;
        Suit suit;
}

void printCard(in Card card) {
        final switch(card.value) {
                case Value.ACE:
                        write("A");
                        break;
case Value.TWO, Value.THREE, Value.FOUR, Value.FIVE, Value.SIX, Value.SEVEN, Value.EIGHT, Value.NINE, Value.TEN:
                        writef("%d", card.value);
                        break;
                case Value.JACK:
                        write("J");
                        break;
                case Value.QUEEN:
                        write("Q");
                        break;
                case Value.KING:
                        write("K");
                        break;
        }
        final switch(card.suit) {
                case Suit.HEARTS:
                        write("♡");
                        break;
                case Suit.DIAMONDS:
                        write("♢");
                        break;
                case Suit.CLUBS:
                        write("♣");
                        break;
                case Suit.SPADES:
                        write("♠");
                        break;
        }
        write("\n");
}

int main() {
        auto card = Card(Value.JACK, Suit.CLUBS);
        printCard(card);
        return 0;
}

Reply via email to