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

Array patterns #141

Open
AnthonyDGreen opened this issue Jul 13, 2017 · 2 comments
Open

Array patterns #141

AnthonyDGreen opened this issue Jul 13, 2017 · 2 comments

Comments

@AnthonyDGreen
Copy link
Contributor

This builds on proposal #124 like proposals #139 and #140 but for the array literal syntax:

Select Case arr
    Case Match {} ' Empty.
    Case Match {single} ' One element
    Case Match {first, second} ' This could work.
    Case Else ' Must have more than 2 elements.
End Select

Would it work for arbitrary collections? IEnumerables?
Unanswered.

Would it be very valuable to have a sort of "the rest" syntax
I could imagine a syntax like:

Case Match {first, ...}

That captures the first element but allows for sizes greater than one but that seems of extremely marginal benefit for syntax compared with just grabbing the array and manually extracting values.

@reduckted
Copy link
Contributor

Unless this also supported IEnumerable efficiently (as in, not enumerating multiple times), I don't see it being of much benefit. Most public APIs use IEnumerable (or something similar that's not an array). In fact, Microsoft's own guidelines state not to use arrays publicly.

✓ DO prefer using collections over arrays in public APIs.

Maybe an alternative (or even in addition to the matching) would be something like TypeScript/ES6 array destructuring.

Dim collection As IEnumerable(Of Integer)
Dim first As Integer
Dim second As Integer

[ first, second ] = collection

The actual syntax is debatable, but the concept is what I'm getting at. That would basically get converted to:

counter = 0
For Each item In collection
    Select Case counter
        Case 0
            first = item
        Case 1
            second = item
            Exit For
    End Select

    counter += 1
Next item

@AdamSpeight2008
Copy link
Contributor

Rest syntax is useful for recursive definitions, especially if tail recursive optimised.
Or using a Functional Language List.

Function [Me]( nums As Int(), Optional sum As Int = 0 ) As Int
  '  NOTE: I'm using Match instead of Case, this indicates ALL clause are to considered match syntax.
  Select Match nums
    Case { num }             : return sum + num
    Case { num , rest: ... } : return [Me](rest, sum+num)
    Case {}                  : return sum
  End Select
End Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants