Hello again =)!

I'm doing advent of code to improve my Nim and encountered a problem today 
trying to create a string type with specific constraints. I'd like to 
understand type constraints if they exist, not just for this problem but 
generally.

Today's problem deals with hands of poker represented as 5 character strings 
containing any of the following cards/chars {'A', 'K', 'Q', 'J', 'T', '9', ..., 
'2'}. So a valid hand would look like 'AKT33' for example, while non valid hand 
would look like 'AKT333' (6 chars) or 'ABCDE' (wrong character set).

I tried with concepts:
    
    
    type
      HandLike = concept x
        x is string
        len(x) == 5
        HashSet(x) == {"A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", 
"2"}
    
    
    Run

But after reading [this 
question](https://stackoverflow.com/questions/71242378/nim-how-to-constrain-an-existing-type),
 in which they have a very similar problem (DNA strings), it seems that's not 
what concepts are for.

My second idea is to do something like:
    
    
    type
      Card = enum
        CA, CK, CQ, CJ, CT, C9, C8, C7, C6, C5, C4, C3, C2, C1
      Hand = object
        card1, card2, card3, card4, card5: Card
    
    
    Run

But it's going to require a lot of boilerplate to convert, compared to just 
applying constraints on a string type. Also in an hypothetical where I have 
more than 5 cards, for example to represent a 52 cards deck, it starts to 
become unwieldy.

Their answer to the DNA problem was just to parse it with a distinct string but 
I'd like to encode the constraints directly into the type, is it possible?

Reply via email to