Haskell-like as-patterns in switch statements


(Ionuț G. Stan) #1

Hi all,

I'm unsure if this is the appropriate venue to discuss this, so apologies if it's not.

I was wondering if the issue of supporting as-patterns in switch statements has been brought up before or not, and what was the outcome of that.

By as-patterns I mean the ability to bind sub-patterns to identifiers. See this question of mine on StackOverflow for an example: http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041

Maybe an equal sign could be used to denote that, so my example above would become:

     switch self {
     case .Int: return "int"
     case .Fun(let p = .Fun, let r): return "(\(p)) -> \(r)"
     case .Fun(let p, let r): return "(\(p) -> \(r))"
     }

Note the `let p = .Fun` part.

Alternatively, enhancing the `where` clause of a `case` to support the same features as an `if case` expression, would be an acceptable choice, too?

     case .Fun(let p, let r) where case .Fun = p:
         return "(\(p)) -> \(r)"

Thanks for reading.

···

--
Ionuț G. Stan | http://igstan.ro | http://bucharestfp.ro


(Karl) #2

I think the second (using where clauses to provide a more specific constraint) is probably more consistent with the rest of the language.

   switch self {
   case .Int:
     ...
   case .Fun(let outer_p, let r) where case .Fun(let inner_p,_) = outer_p, case .Int = inner_p:
     ...
   case .Fun(let p, let r) where case .Fun(_,_) = p:
     ...
   case .Fun(let p, let r):
     ...
   }

Looks pretty good IMO, although I do wonder if we couldn’t drop the second “case”.

- Karl

···

On 23 Apr 2017, at 15:55, Ionuț G. Stan via swift-evolution <swift-evolution@swift.org> wrote:

Hi all,

I'm unsure if this is the appropriate venue to discuss this, so apologies if it's not.

I was wondering if the issue of supporting as-patterns in switch statements has been brought up before or not, and what was the outcome of that.

By as-patterns I mean the ability to bind sub-patterns to identifiers. See this question of mine on StackOverflow for an example: http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041

Maybe an equal sign could be used to denote that, so my example above would become:

   switch self {
   case .Int: return "int"
   case .Fun(let p = .Fun, let r): return "(\(p)) -> \(r)"
   case .Fun(let p, let r): return "(\(p) -> \(r))"
   }

Note the `let p = .Fun` part.

Alternatively, enhancing the `where` clause of a `case` to support the same features as an `if case` expression, would be an acceptable choice, too?

   case .Fun(let p, let r) where case .Fun = p:
       return "(\(p)) -> \(r)"

Thanks for reading.

--
Ionuț G. Stan | http://igstan.ro | http://bucharestfp.ro
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jon Hull) #3

You can also do:

switch self {
   case .Int: return "int"
   case .Fun(.Fun(let p), let r): return "(\(p)) -> \(r)"
   case .Fun(let p, let r): return "(\(p) -> \(r))"
}

···

On Apr 23, 2017, at 6:55 AM, Ionuț G. Stan via swift-evolution <swift-evolution@swift.org> wrote:

Hi all,

I'm unsure if this is the appropriate venue to discuss this, so apologies if it's not.

I was wondering if the issue of supporting as-patterns in switch statements has been brought up before or not, and what was the outcome of that.

By as-patterns I mean the ability to bind sub-patterns to identifiers. See this question of mine on StackOverflow for an example: http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041

Maybe an equal sign could be used to denote that, so my example above would become:

   switch self {
   case .Int: return "int"
   case .Fun(let p = .Fun, let r): return "(\(p)) -> \(r)"
   case .Fun(let p, let r): return "(\(p) -> \(r))"
   }

Note the `let p = .Fun` part.

Alternatively, enhancing the `where` clause of a `case` to support the same features as an `if case` expression, would be an acceptable choice, too?

   case .Fun(let p, let r) where case .Fun = p:
       return "(\(p)) -> \(r)"

Thanks for reading.

--
Ionuț G. Stan | http://igstan.ro | http://bucharestfp.ro
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Ionuț G. Stan) #4

You can also do:

switch self {
   case .Int: return "int"
   case .Fun(.Fun(let p), let r): return "(\(p)) -> \(r)"
   case .Fun(let p, let r): return "(\(p) -> \(r))"
}

I didn't know you can do that and it seems consistent with other features of the language. However, it doesn't do what I was aiming for. In your example `p` will be bound to a `(Type, Type)`, not a `Type`. So the string result, instead of being `(int -> int) -> int` is actually `((int, int)) -> int`.

Anyway, I've learned something new, so thanks!

···

On 24/04/2017 10:42, Jonathan Hull wrote:

On Apr 23, 2017, at 6:55 AM, Ionuț G. Stan via swift-evolution <swift-evolution@swift.org> wrote:

Hi all,

I'm unsure if this is the appropriate venue to discuss this, so apologies if it's not.

I was wondering if the issue of supporting as-patterns in switch statements has been brought up before or not, and what was the outcome of that.

By as-patterns I mean the ability to bind sub-patterns to identifiers. See this question of mine on StackOverflow for an example: http://stackoverflow.com/questions/43566117/haskell-like-as-patterns-in-swift/43567041#43567041

Maybe an equal sign could be used to denote that, so my example above would become:

   switch self {
   case .Int: return "int"
   case .Fun(let p = .Fun, let r): return "(\(p)) -> \(r)"
   case .Fun(let p, let r): return "(\(p) -> \(r))"
   }

Note the `let p = .Fun` part.

Alternatively, enhancing the `where` clause of a `case` to support the same features as an `if case` expression, would be an acceptable choice, too?

   case .Fun(let p, let r) where case .Fun = p:
       return "(\(p)) -> \(r)"

Thanks for reading.

--
Ionuț G. Stan | http://igstan.ro | http://bucharestfp.ro
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Ionuț G. Stan | http://igstan.ro | http://bucharestfp.ro