Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range / tuple enums #1493

Open
nixpulvis opened this Issue Feb 8, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@nixpulvis
Copy link

nixpulvis commented Feb 8, 2016

I've been thinking about writing a strict type for a deck of cards in rust (for example), and I've found myself really wanting this feature. The idea is to allow ranges or tuples in the definition of an enum. This would allow Rust to express (easily) larger numbers of variants for enums. This would play nicely with enum refinement typing, as I've read about elsewhere.

enum CardValue(0...13);
// Or
enum CardValue(1,2,3,4,5,6,7,8,9,10,11,12,13);

enum Suit {
    Hearts,
    Diamonds,
    Clubs,
    Spades,
}

struct Card {
    value: CardValue,
    suit: Suit,
}
// Or
struct Card {
    value: enum(0...13),
    suit: Suit,
}
// Or
struct Card {
    value: (0...13),
    suit: Suit,
}
@ticki

This comment has been minimized.

Copy link
Contributor

ticki commented Feb 8, 2016

I ask: What you're suggesting is type-level ranges? If so, are you proposing ability to do arithmetic operations on them?

@KalitaAlexey

This comment has been minimized.

Copy link

KalitaAlexey commented Feb 8, 2016

@nixpulvis How do you want to assign Card.value?

Card.value = 12;

or

Card.value = CardValue::12;

I'd like to write second variant. But I don't know is it possible?

@nixpulvis

This comment has been minimized.

Copy link
Author

nixpulvis commented Feb 8, 2016

@KalitaAlexey I could see a number of ways possibly. These are the two options that most jump out at me. One treats the created enum very much like enums are treated today, the other required the compiler to know that 1 is somehow a valid value for the enum. I'd be happy with the first option to start, then working toward getting the second option maybe.

enum CardValue(0...13);

struct Card {
    value: CardValue,
    suit: Suit,
}

let card = Card {
    value: CardValue(1),
    suit: Suit::Clubs,
};

// Or

struct Card {
    value: (0...13),
    suit: Suit,
}

let card = Card {
    value: 1,
    suit: Suit::Hearts,
};

@nrc nrc added the T-lang label Aug 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.