Freigeben über


Req20: Range expressions

[This post is part of a series, "wish-list for future versions of VB"]

 

IDEA 1: Allow range expressions as a literal for IEnumerable. For instance,

        ' This expression

        Dim x = 5 To 14

        ' should be syntactic sugar for this one

        Dim x = Enumerable.Range(5, 10)

 

IDEA 2: Allow "in range" expressions. For instance,

' These conditions

If y In 50 To 100 Then ...

        If 50 <= y <= 100 Then ...

' should be syntactic sugar for this one

        If 50 <= y AndAlso y <= 100 Then

 

 

Provisional evaluation from VB team: The syntax for IEnumerable.Range isn't much of an improvement. As for the range-expression-tests, "50 <= y <= 100" already has a different meaning in VB, and "y In 50 To 100" isn't any clearer than the long version. We're not persuaded by these ideas.

Comments

  • Anonymous
    February 16, 2010
    Different, SQL-like, syntax can be employed for range testing: Dim result = a Between7 And 11 Also SQL-like IN could be useful - I missed it so many times: Dim result = a In(0, 15, 33, 100)

  • Anonymous
    February 16, 2010
    I don’t see a need for range literals, but ‘in range’ expressions could be useful. It is a bit painful to have to spell out the full expression on both sides of the range – especially if the expression is of any length. For example, If x > 0 AndAlso X < 10 Then ... is fine, but a more realistic: If numberOfEntries >m_minimumAllowed  AndAlso numberOfEntries < m_maximumAllowed or If m_minimumAllowed  <= numberOfEntries AndAlso numberOfEntries <= m_maximumAllowed is a bit more unwieldy. It’s obviously much worse when you are comparing an expression as opposed to a simple variable, and creating a temporary variable just to simplify the comparison syntax is not ideal.   Maybe VB could support a SQL-like range expression for comparisons? If numberOfEntries Between m_minimumAllowed  And m_maximumAllowed It’s unambiguous, avoids the repetition, and

  • Anonymous
    February 17, 2010
    The comment has been removed

  • Anonymous
    February 24, 2010
    I quite like the first part of Idea2, i.e. If y In 50 To 100 Then ... which is actually clearer than If 50 <= y AndAlso  y <= 100 Then despite what you suggest, because it avoids the need to repeat "y".  In a real-life program our variable might have a longer name. I don't like        If 50 <= y <= 100 Then ... As you say, it already has a different meaning.

  • Anonymous
    February 26, 2010
    New syntax unnecessary. Between is a good candidate for a library function. Or is it there already?

  • Anonymous
    March 13, 2010
    I don't like the Dim x = 5 To 14 It seems extremely ambiguous to me.  Does it give me an array, or an IEnumerable<int>, or what? I do like the idea of a Between keyword, so you could say If y Between 50 And 100 The awkward bit would be figuring out how to designate inclusive vs. exclusive (or inclusive on one wide and exclusive on the other side).  Given how And and Or came out, I fear we'd end up with If y BetweenInclusive 50 And 100 and If y BetweenExclusive 50 And 100 shudder The In idea would be nice also: If y In(0, 50, 100, 37) I often end up writing a method or extension method like this.

  • Anonymous
    April 05, 2010
    In SQL, Between is implicitly inclusive, so why not have a keyword Between in VB that is also implicitly inclusive, then maybe InBetween or something similar as a way to make it exclusive. Or vise-versa. But I can definitely say this feature would be used quite often.

  • Anonymous
    July 16, 2011
    Here's what I'd do, if we're playing fantasy language: If x : (min, max] Then Basically specifying the range of the variable through mathematical expression.  The ':' specifies 'within the range, '(' and ')' mean exclusive, and '[' and ']' mean inclusive.  So If start <= x AndAlso x < end would be represented by:   If x : [start, end) Then