As a fellow functional programming aficionado, I'd like this very much, too. From lurking here, I think something like this is of low priority at the moment since it is and additive feature.
Especially head/tail matching looks very interesting. I also like the syntax you suggest here:
case let [first, next...]:
case let [first, last]:
The binding should be more like "[let first, let last]" though, to be more like the tuple matching. For the above also: "case [let head, let tail...]".
···
On 18 Dec 2016, at 06:43, Mathew Sanders via swift-evolution <swift-evolution@swift.org> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in the past.
Curious to hear if a future version of Swift might ever include some sort of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to Arrays as well, but not sure if it fits with future goals for the language.
Yeah, this is something I've wanted to add for a while, along with pattern matching for dictionaries, which together with array matching I think would make a lot of JSON/plist wrangling much more pleasant. Since it's an additive feature, it's taken a backseat to more pressing source- and ABI-breaking changes we've wanted to get out of the way first.
-Joe
···
On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in the past.
Curious to hear if a future version of Swift might ever include some sort of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to Arrays as well, but not sure if it fits with future goals for the language.
On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in the past.
Curious to hear if a future version of Swift might ever include some sort of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to Arrays as well, but not sure if it fits with future goals for the language.
Actually, with tuples, case let (first, last) is equivalent to case (let
first, let last), so the “compact” form should be ok with Array pattern
matching.
···
2016-12-19 3:09 GMT+03:00 Lucas Neiva via swift-evolution < swift-evolution@swift.org>:
case let [first, next...]:
> case let [first, last]:
The binding should be more like "[let first, let last]" though, to be more
like the tuple matching. For the above also: "case [let head, let tail...]".
On Dec 22, 2016, at 9:46 AM, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:
On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in the past.
Curious to hear if a future version of Swift might ever include some sort of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to Arrays as well, but not sure if it fits with future goals for the language.
Yeah, this is something I've wanted to add for a while, along with pattern matching for dictionaries, which together with array matching I think would make a lot of JSON/plist wrangling much more pleasant. Since it's an additive feature, it's taken a backseat to more pressing source- and ABI-breaking changes we've wanted to get out of the way first.
Yeah, this is something I've wanted to add for a while, along with pattern matching for dictionaries, which together with array matching I think would make a lot of JSON/plist wrangling much more pleasant.
Regarding the "some-day" list (or "deferred" proposals): It has been
discussed many times before, and Chris also agreed that it would be
useful[1], but nobody seems to have made it happen. So I have created a
trello board:
Hopefully we can maintain it collectively, and it should be a handy quick
reference to previous discussions about specific subjects. I've added a few
lists/cards to get started, including a link to this discussion about
pattern matching. There's a README on the board that contains a link to
gain add/edit access to the board.
On Fri, 23 Dec 2016 at 04:38 David Sweeris via swift-evolution < swift-evolution@swift.org> wrote:
On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution < > swift-evolution@swift.org> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in
the past.
Curious to hear if a future version of Swift might ever include some sort
of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to
Arrays as well, but not sure if it fits with future goals for the language.
I'd be excited to see a feature like this for arrays, dictionaries, and array-like items such as strings. I know that this cannot be addressed for a while, but I'd love to see the idea formalized a bit and placed on a list somewhere so it doesn't just disappear into the aether.
It's a pity we don't have a formal registry for "Some Day" items like this that get mentioned on-list, but don't properly fit into Jira or the SE pull-request queue.
-- E
···
On Dec 22, 2016, at 8:43 PM, Robert Widmann via swift-evolution <swift-evolution@swift.org> wrote:
On Dec 22, 2016, at 9:46 AM, Joe Groff via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
I've just joined the list (hi!) so not sure if this has been discussed in the past.
Curious to hear if a future version of Swift might ever include some sort of pattern-matching extended to Arrays. Maybe something like the following:
let numbers: [Int]
switch numbers {
case :
// to match an empty array
case [1, 2, 3]:
// to match array with specific values
case [_, _, _]:
// to match array with 3 Ints
case let [first, last]:
// match a 2 element array and bind first and last elements
case let [first, next...]:
// to match array with at least one element
// bind first element, and bind other elements (could be empty)
// first: Int, next: [Int]
}
I love the pattern matching on tuples, and would love to see if extend to Arrays as well, but not sure if it fits with future goals for the language.
Yeah, this is something I've wanted to add for a while, along with pattern matching for dictionaries, which together with array matching I think would make a lot of JSON/plist wrangling much more pleasant. Since it's an additive feature, it's taken a backseat to more pressing source- and ABI-breaking changes we've wanted to get out of the way first.
My personal favorite design for user-extensible pattern syntax is F#'s "active pattern" feature, which lets you declare a set of mutual exclusive, total or partial conditions along with a function body that computes which condition holds. For the specific case of container patterns, though, I feel like it's worth keeping a close association with Collection semantics, so that:
- matches when the incoming collection's startIndex == endIndex,
- [<pattern>, <pattern-list>] fails if startIndex == endIndex, or else matches collection[startIndex] against <pattern> and recursively matches collection[startIndex.advancedBy(1)..<endIndex] against [<pattern-list>]
- [<pattern>...] matches the remaining collection against <pattern>
-Joe
···
On Dec 22, 2016, at 7:43 PM, Robert Widmann <devteam.codafi@gmail.com> wrote:
Do you think it would make sense to make this an official part of swift-evolution? Perhaps an analog to the “commonly rejected” list?
-Chris
···
On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution <swift-evolution@swift.org> wrote:
Regarding the "some-day" list (or "deferred" proposals): It has been discussed many times before, and Chris also agreed that it would be useful[1], but nobody seems to have made it happen. So I have created a trello board: Trello
Hopefully we can maintain it collectively, and it should be a handy quick reference to previous discussions about specific subjects. I've added a few lists/cards to get started, including a link to this discussion about pattern matching. There's a README on the board that contains a link to gain add/edit access to the board.
+1 on that, I'd love to have a peek ahead to see the direction swift might
be taking in the long-term... and I actually looked for something like this
before I mailed the list.
···
On 17 January 2017 at 01:21, Chris Lattner <sabre@nondot.org> wrote:
On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution < > swift-evolution@swift.org> wrote:
Regarding the "some-day" list (or "deferred" proposals): It has been
discussed many times before, and Chris also agreed that it would be
useful[1], but nobody seems to have made it happen. So I have created a
trello board: Trello
Hopefully we can maintain it collectively, and it should be a handy quick
reference to previous discussions about specific subjects. I've added a few
lists/cards to get started, including a link to this discussion about
pattern matching. There's a README on the board that contains a link to
gain add/edit access to the board.
This is really cool. I've never used F#, so thanks for pointing this out.
active-patterns Extra points for the name (| "banana clips" |).
I've had something like this on my backburner-wishlist for a while.
Customizable patterns/bindings could be very powerful.
One use case I had in mind: writing parsers for binary formats such as
msgpack <http://msgpack.org/index.html>\. For instance, msgpack encodes a
uint16 as 0xcd + high byte + low byte. If the 0xcd could be given as a
parameter to the "active pattern", you could imagine writing something like
match msgpackStream { // a raw byte stream
// Scan 3 bytes, the first of which is equal to 0xcd.
// Theoretical syntax allowing a custom stream-scanner object to fill in
the two variable bindings
// by reading bytes from the stream. If the first byte were not 0xcd, the
match would fail.
case [0xcd, let hi, let lo]:
return UInt16(hi) << 8 | UInt16(lo)
...
}
···
On Tue, Jan 3, 2017 at 10:10 AM, Joe Groff via swift-evolution < swift-evolution@swift.org> wrote:
On Dec 22, 2016, at 7:43 PM, Robert Widmann <devteam.codafi@gmail.com> > wrote:
My personal favorite design for user-extensible pattern syntax is F#'s
"active pattern" feature, which lets you declare a set of mutual exclusive,
total or partial conditions along with a function body that computes which
condition holds. For the specific case of container patterns, though, I
feel like it's worth keeping a close association with Collection semantics,
so that:
- matches when the incoming collection's startIndex == endIndex,
- [<pattern>, <pattern-list>] fails if startIndex == endIndex, or else
matches collection[startIndex] against <pattern> and recursively matches
collection[startIndex.advancedBy(1)..<endIndex] against [<pattern-list>]
- [<pattern>...] matches the remaining collection against <pattern>
+1 on that, I'd love to have a peek ahead to see the direction swift might be taking in the long-term... and I actually looked for something like this before I mailed the list.
On 17 January 2017 at 01:21, Chris Lattner <sabre@nondot.org> wrote:
On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution <swift-evolution@swift.org> wrote:
Regarding the "some-day" list (or "deferred" proposals): It has been discussed many times before, and Chris also agreed that it would be useful[1], but nobody seems to have made it happen. So I have created a trello board: Trello
Hopefully we can maintain it collectively, and it should be a handy quick reference to previous discussions about specific subjects. I've added a few lists/cards to get started, including a link to this discussion about pattern matching. There's a README on the board that contains a link to gain add/edit access to the board.