Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Less painful range construction #25

Closed

Description

@jeff-davis

https://gist.github.com/sfackler/4c4e93f896e881fae811

I like that solution there. It is reasonably readable, does compile-time checking, and matches the postgres constructors.

However, it was also suggested on IRC to use something more like:

range!('[' a, b ')')

which is also acceptable to me.

Activity

  1. sfackler commented on Jan 2, 2014

    @sfackler
    Collaborator

    I kind of like the range!('[' a, b ')') syntax. Would e.g. range!('(' , b ')') be the best syntax for a half-open range?

  2. jeff-davis commented on Jan 2, 2014

    @jeff-davis
    ContributorAuthor

    That's a good point: it does seem to extend a little better to unbounded ranges.

    Another thought I had is to use textual tokens like II, EI, IE, EE. That is a little awkward to read though and doesn't seem to work naturally for unbounded ranges.

  3. jeff-davis commented on Jan 2, 2014

    @jeff-davis
    ContributorAuthor

    I tried implementing it that way, and I ran into a problem. The ranges with no upper bound work fine, but the ranges with no lower bound won't compile and I don't understand why.

    macro_rules! range(
       ('(' $low:expr, $high:expr ')') => ("(A,B)");
       ('(' $low:expr, $high:expr ']') => ("(A,B]");
       ('[' $low:expr, $high:expr ')') => ("[A,B)");
       ('[' $low:expr, $high:expr ']') => ("[A,B]");
       ('('          , $high:expr ')') => ("(-inf,B)");
       ('('          , $high:expr ']') => ("(-inf,B]");
       ('(' $low:expr,            ')') => ("(A,inf)");
       ('[' $low:expr,            ')') => ("[A,inf)");
       ($low: expr, $high:expr) => (
          range!('[' $low, $high ')')
       );
    )
    
    println!("{}",    range!('('  , 2 ')')   ); // error
    println!("{}",    range!('(' 1,   ')')   ); // works fine
    
  4. sfackler commented on Jan 2, 2014

    @sfackler
    Collaborator

    It tries to parse an expression first, fails and doesn't try the next option. Moving the no-lower-bound cases to the top appear to fix it.

  5. jeff-davis commented on Jan 2, 2014

    @jeff-davis
    ContributorAuthor

    Yes, thank you, I had just realized that and was about to update my comment.

    +1 to this approach

    We'll also need a basic constructor for an empty range and a range unbounded on both sides.

  6. sfackler commented on Jan 2, 2014

    @sfackler
    Collaborator

    There's already Range::empty() to construct the empty range, but we might as well add range!(empty) and range!('(',')') for completeness.

  7. sfackler commented on Jan 2, 2014

    @sfackler
    Collaborator

    I've added a macro to types/range.rs. You'll have to copy/paste it into your source file for now until rust-lang/rust#11151 is finished up.

  8. added a commit that references this issue on Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions