{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/1419","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/1419/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/1419/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/1419/events","html_url":"https://github.com/dotnet/roslyn/issues/1419","id":63268097,"number":1419,"title":"[C# feature] Stronger inference of a common type of a set of expressions","user":{"login":"dsaf","id":3845650,"avatar_url":"https://avatars.githubusercontent.com/u/3845650?v=3","gravatar_id":"","url":"https://api.github.com/users/dsaf","html_url":"https://github.com/dsaf","followers_url":"https://api.github.com/users/dsaf/followers","following_url":"https://api.github.com/users/dsaf/following{/other_user}","gists_url":"https://api.github.com/users/dsaf/gists{/gist_id}","starred_url":"https://api.github.com/users/dsaf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsaf/subscriptions","organizations_url":"https://api.github.com/users/dsaf/orgs","repos_url":"https://api.github.com/users/dsaf/repos","events_url":"https://api.github.com/users/dsaf/events{/privacy}","received_events_url":"https://api.github.com/users/dsaf/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"}],"state":"open","locked":false,"assignee":{"login":"MadsTorgersen","id":6677272,"avatar_url":"https://avatars.githubusercontent.com/u/6677272?v=3","gravatar_id":"","url":"https://api.github.com/users/MadsTorgersen","html_url":"https://github.com/MadsTorgersen","followers_url":"https://api.github.com/users/MadsTorgersen/followers","following_url":"https://api.github.com/users/MadsTorgersen/following{/other_user}","gists_url":"https://api.github.com/users/MadsTorgersen/gists{/gist_id}","starred_url":"https://api.github.com/users/MadsTorgersen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MadsTorgersen/subscriptions","organizations_url":"https://api.github.com/users/MadsTorgersen/orgs","repos_url":"https://api.github.com/users/MadsTorgersen/repos","events_url":"https://api.github.com/users/MadsTorgersen/events{/privacy}","received_events_url":"https://api.github.com/users/MadsTorgersen/received_events","type":"User","site_admin":false},"milestone":null,"comments":7,"created_at":"2015-03-20T17:19:10Z","updated_at":"2015-07-24T14:05:06Z","closed_at":null,"body":"This code will currently not work:\r\n\r\n```csharp\r\npublic class Sample\r\n{\r\n    public static void Main()\r\n    {\r\n        //https://msdn.microsoft.com/en-us/library/bb384226.aspx\r\n        //Compiler Error CS0826: No best type found for implicitly typed array.\r\n        var animalsArray = new[] { new Dog(), new Cat() };\r\n    }\r\n}\r\n\r\npublic abstract class Animal {}\r\n\r\npublic class Dog : Animal {}\r\n\r\npublic class Cat : Animal {}\r\n```\r\n\r\nIt could go one step further and assume the first common type in the inheritance hierarchy.\r\nSeveral situations possible:\r\n\r\n1. Most straightforward - single common parent type - see above.\r\n2. Primitive types - needs explicit manual resolution.\r\n3. Nullable and non-nullable - should nullable be assumed?\r\n4. Only several common interfaces are shared - it's better to be explicit in this case rather than picking the first one.\r\n5. One common interface and a common parent type are shared - always assume the parent type, unless it's object?\r\n6. Several common interfaces and a common parent type are shared - always assume the parent type. \r\n7. Generics - probably more tricky, and a separate issue - see #1470 to start.\r\n\r\nRelated SO question: http://stackoverflow.com/questions/2799902/implicit-typing-of-arrays-that-implement-interfaces."},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/124533565","html_url":"https://github.com/dotnet/roslyn/issues/1419#issuecomment-124533565","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/1419","id":124533565,"user":{"login":"jveselka","id":1810119,"avatar_url":"https://avatars.githubusercontent.com/u/1810119?v=3","gravatar_id":"","url":"https://api.github.com/users/jveselka","html_url":"https://github.com/jveselka","followers_url":"https://api.github.com/users/jveselka/followers","following_url":"https://api.github.com/users/jveselka/following{/other_user}","gists_url":"https://api.github.com/users/jveselka/gists{/gist_id}","starred_url":"https://api.github.com/users/jveselka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jveselka/subscriptions","organizations_url":"https://api.github.com/users/jveselka/orgs","repos_url":"https://api.github.com/users/jveselka/repos","events_url":"https://api.github.com/users/jveselka/events{/privacy}","received_events_url":"https://api.github.com/users/jveselka/received_events","type":"User","site_admin":false},"created_at":"2015-07-24T14:05:06Z","updated_at":"2015-07-24T14:05:06Z","body":"@dsaf \r\nImagine you have this code:\r\n```C#\r\n//correctly inferred as Vegetable[]\r\nvar vegetable = new[] { new Carrot(), new Cucumber() }\r\n//uses Person.Eat(Vegetable v) overload\r\nforeach (var v in vegetable) person.Eat(v);\r\n```\r\nThen you try to add Tomato forgeting that it is technically a Fruit\r\n```C#\r\n//infers Object[] assuming that Vegetable and Fruit have no other common base class\r\nvar vegetable = new[] { new Carrot(), new Cucumber(), new Tomato() }\r\n//error: no overload Person.Eat(Object o) found\r\nforeach (var v in vegetable) person.Eat(v); \r\n```\r\nSo error that should have appeared at declaration appears further in the code as different error.\r\n\r\nI can even imagine cases where similar mistake results in runtime error and I fear some developers will rather fall back to using explicitly typed locals rather than dealing with these problems."}}