News for hackers. Stuff that matters.
Re:Clojure vs. Scala by brudgers on Tuesday December 24, 2013 @12:50PM (#6960311)
The author's point isn't that int-or-string is a good choice, it's that there are a lot of choices and none of them allow natural abstraction over a deck of cards, the static typing encourages the abstraction down to a lower level - like ints or into a mashup like string-or-int.

And this is the really important point, this pushing the abstraction down to a lower level causes it to leak into the implementation of card games in ways that make the card games themselves harder to reason about.

Decks of cards have Kings, not 13's, and using 13 to represent the King is of type leaky abstraction. It's a muddy road leading into the swamp.

When I implement blackjack I get to write little balls of mud:

    If card = 13 then 10
And in my card guessing game

    If card = 13 and guess = King then "Yes, the card is a King!"
All that for a warning when I don't have a card 7, which is going to make Pinochle have to work around the abstraction - probably by bypassing the entire implementation when it chokes on two Jacks of Diamonds etc. in a deck.
Re:Clojure vs. Scala by lmm on Wednesday December 25, 2013 @05:01AM (#6962721)
I said the enum-style approach is what you want for something like that. "case KING | QUEEN | JACK | TEN => 10; case ValueCard(x) => x" is just as readable as anything you'd do in clojure, and the warning (not error - you can ignore it if your game really doesn't need to handle that card) if you forgot one is valuable.
Re:Clojure vs. Scala by brudgers on Wednesday December 25, 2013 @11:56AM (#6963383)
The enum is a bad abstraction because when I play GoFish, the computer asks, "Do you have any 13's?" and when I ask, "Do you have any Jacks?", I am always told to GoFish. It's an abstraction that sometimes returns a meaningful value, and other times doesn't, and the abstraction is built to optimize exactly the cases that are easy for me to reason about when writing a program and returns something that only looks correct in all the difficult cases, such as the Ace and so I wind up having to do all the heavy lifting myself anyway but only after having been misled to the wrong assumption that the abstraction was robust and well thought out.

There's no value to a warning that's irrelevant and perhaps even negative value if it encourages the idea of adding a wildcard last case just so I don't have it interrupting my thoughts each and every time I build my program since doing so opens the door to very run time errors it was supposed to prevent or conversely creates the habit of ignoring the compiler warnings in the very worst situation - when I think I know what I am doing.

If enums were good enough and I wanted to ignore compiler warnings, I could have used C.

Re:Clojure vs. Scala by lmm on Wednesday December 25, 2013 @04:05PM (#6964241)
> The enum is a bad abstraction because when I play GoFish, the computer asks, "Do you have any 13's?" and when I ask, "Do you have any Jacks?", I am always told to GoFish.

What? No it doesn't. Scala case objects don't have numerical values.