{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/812","repository_url":"https://api.github.com/repos/dotnet/csharplang","labels_url":"https://api.github.com/repos/dotnet/csharplang/issues/812/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/csharplang/issues/812/comments","events_url":"https://api.github.com/repos/dotnet/csharplang/issues/812/events","html_url":"https://github.com/dotnet/csharplang/issues/812","id":249506193,"number":812,"title":"Proposal: allow comparison operators in switch case labels C#","user":{"login":"gafter","id":3804346,"avatar_url":"https://avatars3.githubusercontent.com/u/3804346?v=4","gravatar_id":"","url":"https://api.github.com/users/gafter","html_url":"https://github.com/gafter","followers_url":"https://api.github.com/users/gafter/followers","following_url":"https://api.github.com/users/gafter/following{/other_user}","gists_url":"https://api.github.com/users/gafter/gists{/gist_id}","starred_url":"https://api.github.com/users/gafter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gafter/subscriptions","organizations_url":"https://api.github.com/users/gafter/orgs","repos_url":"https://api.github.com/users/gafter/repos","events_url":"https://api.github.com/users/gafter/events{/privacy}","received_events_url":"https://api.github.com/users/gafter/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-08-10T23:43:43Z","updated_at":"2017-08-11T14:43:51Z","closed_at":null,"body":"@JFMous commented on [Sat May 14 2016](https://github.com/dotnet/roslyn/issues/11312)\n\nIt would be nice if the case labels in switch statement supported comparison operators, much like the Select Case statement in Visual Basic does.\n\nNow, there are only two options for the switch labels:\n\n> switch-label:\n> case _constant-expression_ :\n> default :\n\nThis could be extended to:\n\n> case [< | > | <= | >=] _constant-expression_ [ to ] _constant-expression_ :\n> default :\n\nSo you could write code as in this example:\n\n```\nint iq = DoIqTest();\n\nswitch (iq)\n{\n    case <= 69:\n        ProcedureExtremelyLow();\n        break;\n    case 70 to 79:\n        BorderlineProcedure();\n        break;\n    case 80 to 89:\n        LowAverageProcedure();\n        break;\n    case 90 to 99:\n    case 101 to 109:\n        AverageProcedure();\n        break;\n    case 100:\n        ExactlyMedianProcedure();\n        break;\n    case 110 to 119:\n        HighAverageProcedure();\n        break;\n    case 120 to 129:\n         SuperiorProcedure();\n         break;\n    case >= 130:\n        VerySuperiorProcedure();\n        break;\n}\n\n```\n\nThe 'to' keyword would be used to specify a range. In the statement `switch (value)`, `case x to y:` would be equivalent to the boolean expression `value >= x && value <= y`\n\n\n---\n\n@HaloFour commented on [Sat May 14 2016](https://github.com/dotnet/roslyn/issues/11312#issuecomment-219259783)\n\nCase guards in pattern matching #206 will open this up to allowing for arbitrary conditions:\n\n``` cs\nint iq = DoIqTest();\n\nswitch (iq)\n{\n    case * when iq <= 69:\n        ProcedureExtremelyLow();\n        break;\n    case * when iq <= 79:\n        BorderlineProcedure();\n        break;\n    case * when iq <= 89\n        LowAverageProcedure();\n        break;\n    case * when iq == 100:\n        ExactlyMedianProcedure();\n        break;\n    case * when iq <= 109:\n        AverageProcedure();\n        break;\n    case * when iq <= 119:\n        HighAverageProcedure();\n        break;\n    case * when iq <= 129:\n         SuperiorProcedure();\n         break;\n    default:\n        VerySuperiorProcedure();\n        break;\n}\n```\n\nThe wildcard pattern would match on anything but then the case guard would contain the specific conditions.  They would be evaluated in lexical order.\n\n\n---\n\n@alrz commented on [Sun May 15 2016](https://github.com/dotnet/roslyn/issues/11312#issuecomment-219293994)\n\nwould it make sense to make `case` optional or perhaps switch expression?\n\n``` cs\nswitch {\n  when id <= 69: ... break;\n  ...\n}\n```\n\nmight be preferrable over if else.\n\n\n---\n\n@bondsbw commented on [Mon May 16 2016](https://github.com/dotnet/roslyn/issues/11312#issuecomment-219612182)\n\n@alrz I like that somewhat, feels like a reasonable simplification.\n\nBut I feel this may be even more useful in pattern matching.  In match expressions, the roughly equivalent form might be:\n\n``` c#\nvar x = match\n{\n    when iq <= 69: ProcedureExtremelyLow(),\n    when iq <= 79: BorderlineProcedure(),\n    when iq <= 89: LowAverageProcedure(),\n    when iq <= 99 || (iq <= 109 && iq >= 101): AverageProcedure(),\n    when iq == 100: ExactlyMedianProcedure(),\n    when iq <= 119: HighAverageProcedure(),\n    when iq <= 129: SuperiorProcedure(),\n    default: VerySuperiorProcedure()\n};\n```\n\nI like the possibilities but if `iq` is always the comparison, it really feels like it should be the subject of the match expression.  Something like the following feels more like pattern matching (although it would require a bit of rethinking what a relational expression is):\n\n``` c#\nvar x = iq match\n{\n    when <= 69: ProcedureExtremelyLow(),\n    when <= 79: BorderlineProcedure(),\n    when <= 89: LowAverageProcedure(),\n    when == 100: ExactlyMedianProcedure(),\n    when <= 109: AverageProcedure(),\n    when <= 119: HighAverageProcedure(),\n    when <= 129: SuperiorProcedure(),\n    default: VerySuperiorProcedure()\n};\n```\n\nAnd I'm not sure I like changing\n\n``` c#\nwhen iq <= 99 || (iq <= 109 && iq >= 101):\n```\n\ninto something like \n\n``` c#\nwhen <= 99 || (<= 109 && >= 101):\n```\n\n\n---\n\n@Unknown6656 commented on [Mon May 23 2016](https://github.com/dotnet/roslyn/issues/11312#issuecomment-220968107)\n\n@bondsbw, @JFMous : I would like to see something like the following implemented with the `match`-pattern:\n\n```\nFunc<int, string> func1, func2, func3, ...;\n\nint iq = ...;\nstring result = iq match\n{\n    when <= 100: func1,\n    when == 100: func2,\n    when >= 100: func3,\n    // ....\n    default: funcx()\n}(iq);\n```\n\nMeaning, the possibility to use the match-statement inside of expressions.\n\n\n---\n\n@HaloFour commented on [Mon May 23 2016](https://github.com/dotnet/roslyn/issues/11312#issuecomment-220969612)\n\n@Unknown6656 The proposal is that `match` is an expression, so of course you'd be able to use it within other expressions.  You can think of it like a ternary op on steroids.\n\nAs for your specific example, there have been no proposed range patterns.  Nor has there been a proposal to allow for only case guards with an implied wildcard pattern.  So as of now your example would be:\n\n``` cs\nstring result = iq match (\n    case * when iq < 100: func1,\n    case * when iq == 100: func2,\n    case * when iq > 100: func3,\n    case *: funcx\n)(iq);\n```\n\n\n"},"comment":{"url":"https://api.github.com/repos/dotnet/csharplang/issues/comments/321832788","html_url":"https://github.com/dotnet/csharplang/issues/812#issuecomment-321832788","issue_url":"https://api.github.com/repos/dotnet/csharplang/issues/812","id":321832788,"user":{"login":"eyalsk","id":3756641,"avatar_url":"https://avatars3.githubusercontent.com/u/3756641?v=4","gravatar_id":"","url":"https://api.github.com/users/eyalsk","html_url":"https://github.com/eyalsk","followers_url":"https://api.github.com/users/eyalsk/followers","following_url":"https://api.github.com/users/eyalsk/following{/other_user}","gists_url":"https://api.github.com/users/eyalsk/gists{/gist_id}","starred_url":"https://api.github.com/users/eyalsk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyalsk/subscriptions","organizations_url":"https://api.github.com/users/eyalsk/orgs","repos_url":"https://api.github.com/users/eyalsk/repos","events_url":"https://api.github.com/users/eyalsk/events{/privacy}","received_events_url":"https://api.github.com/users/eyalsk/received_events","type":"User","site_admin":false},"created_at":"2017-08-11T14:43:51Z","updated_at":"2017-08-11T14:43:51Z","body":"@DavidArno I agree that `match` expression would be awesome to have sooner than later but don't know if this is the place to discuss priorities, it's a reasonable suggestion, at least in my opinion.\r\n\r\np.s. I wouldn't downvote something just due to priorities, not saying you did but just a side note. 😉 "}}
{"actor":{"display_login":"eyalsk"}}