{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/178","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/178/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/178/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/178/events","html_url":"https://github.com/dotnet/roslyn/issues/178","id":56055681,"number":178,"title":"Pattern matching based on generic type","user":{"login":"colin-jack","id":687016,"avatar_url":"https://avatars.githubusercontent.com/u/687016?v=3","gravatar_id":"","url":"https://api.github.com/users/colin-jack","html_url":"https://github.com/colin-jack","followers_url":"https://api.github.com/users/colin-jack/followers","following_url":"https://api.github.com/users/colin-jack/following{/other_user}","gists_url":"https://api.github.com/users/colin-jack/gists{/gist_id}","starred_url":"https://api.github.com/users/colin-jack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colin-jack/subscriptions","organizations_url":"https://api.github.com/users/colin-jack/orgs","repos_url":"https://api.github.com/users/colin-jack/repos","events_url":"https://api.github.com/users/colin-jack/events{/privacy}","received_events_url":"https://api.github.com/users/colin-jack/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Language%20Design","name":"Area-Language Design","color":"5319e7"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Feature%20Request","name":"Feature Request","color":"fc2929"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Language-C%23","name":"Language-C#","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-30T16:42:24Z","updated_at":"2015-12-24T16:25:44Z","closed_at":null,"body":"One use for pattern matching could be to choose appropriate code path based on a generic type. Say we want to map objects of types Foo and Bar to a new object of type Target. However the code that needs to do the mapping doesn't know if we have a Foo, a Bar, or something else:\r\n\r\n```\r\n        public class Target { ... }\r\n\r\n        public class Foo { ... }\r\n\r\n        public class Bar { ... }\r\n\r\n        public class FooMapper {\r\n            public Target Map(Foo mapFrom) {...}\r\n        }\r\n\r\n        public class BarMapper {\r\n            public Target Map(Bar mapFrom) {...}\r\n        }\r\n        \r\n        private Target Map<TFrom>(TFrom mappingFrom) = TFrom match { \r\n            case Foo => new FooMapper().Map(mappingFrom) // NOTE: No need to cast mappingFrom, we only match the case if its a Foo so no need to do \"FooMapper().Map((Foo)mappingFrom)\"\r\n            case Bar => new BarMapper().Map(mappingFrom)\r\n            default => throw new InvalidOprationException(..); // NOTE: or maybe no match implicitly produces exception\r\n        }\r\n```\r\n\r\nThere are alternatives already but I think this Scala style matching shows whats happening quite cleanly. Obviously you could add type constraints too:\r\n\r\n```\r\n    private Target Map<TFrom>(TFrom mappingFrom) where TFrom: IModel = TFrom match { \r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/167132914","html_url":"https://github.com/dotnet/roslyn/issues/178#issuecomment-167132914","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/178","id":167132914,"user":{"login":"colin-jack","id":687016,"avatar_url":"https://avatars.githubusercontent.com/u/687016?v=3","gravatar_id":"","url":"https://api.github.com/users/colin-jack","html_url":"https://github.com/colin-jack","followers_url":"https://api.github.com/users/colin-jack/followers","following_url":"https://api.github.com/users/colin-jack/following{/other_user}","gists_url":"https://api.github.com/users/colin-jack/gists{/gist_id}","starred_url":"https://api.github.com/users/colin-jack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colin-jack/subscriptions","organizations_url":"https://api.github.com/users/colin-jack/orgs","repos_url":"https://api.github.com/users/colin-jack/repos","events_url":"https://api.github.com/users/colin-jack/events{/privacy}","received_events_url":"https://api.github.com/users/colin-jack/received_events","type":"User","site_admin":false},"created_at":"2015-12-24T16:25:44Z","updated_at":"2015-12-24T16:25:44Z","body":"I'm not sure I follow, you say the example doesn't make sense but your issues seems to just be with the specific syntax. I'll thus assume it is the syntax that's the issue...\r\n\r\nI'm not sure I really follow what you mean about matching type parameter. On generics though, yup it doesn't need to be generic in that case. However at time I logged the issue there was no clear syntax for any of this and for the single example I chose to make the method generic just in case you wanted to apply additional constraints. I've not thought about that aspect much though.\r\n\r\nYour example looks odd to my eyes, I understand it but compared to the Scala matching approach I find it a bit ugly. I haven't kept up with what's being suggested though, so if that switch approach is what's chosen then I'd learn to live with it."}}