{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/6788","repository_url":"https://api.github.com/repos/dotnet/roslyn","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/6788/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/6788/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/6788/events","html_url":"https://github.com/dotnet/roslyn/issues/6788","id":116996721,"number":6788,"title":"Proposal: Asynchronous Construction","user":{"login":"alrz","id":3105979,"avatar_url":"https://avatars.githubusercontent.com/u/3105979?v=3","gravatar_id":"","url":"https://api.github.com/users/alrz","html_url":"https://github.com/alrz","followers_url":"https://api.github.com/users/alrz/followers","following_url":"https://api.github.com/users/alrz/following{/other_user}","gists_url":"https://api.github.com/users/alrz/gists{/gist_id}","starred_url":"https://api.github.com/users/alrz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alrz/subscriptions","organizations_url":"https://api.github.com/users/alrz/orgs","repos_url":"https://api.github.com/users/alrz/repos","events_url":"https://api.github.com/users/alrz/events{/privacy}","received_events_url":"https://api.github.com/users/alrz/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/Discussion","name":"Discussion","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":101,"created_at":"2015-11-15T15:12:36Z","updated_at":"2016-06-13T16:29:38Z","closed_at":null,"body":"Inspired by an [article](http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html) on async initialization:\r\n\r\n> It would be useful to be able to use `await` in a constructor, but this would mean that the constructor would have to return a `Task<T>` representing a value that will be constructed in the future, instead of a constructed value.\r\n\r\nThe point is, to be able to write:\r\n```cs\r\nclass Foo {\r\n    public async Foo() {\r\n        // await\r\n    }\r\n}\r\n```\r\nTo not alter the existing  constructor semantics, the compiler would generate an `async` method corresponding to the `async` constructor and use it to initialize the object.\r\n\r\nFor local variable declaration we use\r\n```cs\r\nvar foo = await new Foo();\r\n```\r\nThis should be integrated with \"async disposables\"  #114 and  #5881 so that when it's out of scope, the disposal should be taken care of.\r\n```cs\r\nusing foo = await new Foo();\r\n```\r\nIt's a compiler error if one called an `async` constructor without `await`. in order to get the involving `Task` you may use the following syntax:\r\n```cs\r\nFoo foo = new Foo(); // compiler error\r\nTask<Foo> foo = new async Foo(); // OK\r\nFoo foo = await new Foo(); // OK\r\n```\r\nFor initializing class fields we do as usual:\r\n```cs\r\nasync class Bar {\r\n    private Foo foo = await new Foo();\r\n}\r\n```\r\nThis makes the whole class `async` because you cannot instantiate this class with any of regular (or, obviously, `async`) constructors without `await new` or `new async` anymore.\r\n\r\nAlso, for async disposable types, the #111 should be taken into account.\r\n```cs\r\nasync class Bar {\r\n    private using Foo foo = await new Foo();\r\n}\r\n```\r\n`async class` can be used also for classes that use `await` on methods in field initialization:\r\n```cs\r\nasync class Baz {\r\n    private Foo foo = await GetFooAsync();   \r\n}\r\n```\r\nThen an `await new` or `new async` shall be needed in order to instantiate the type.\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/225634706","html_url":"https://github.com/dotnet/roslyn/issues/6788#issuecomment-225634706","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/6788","id":225634706,"user":{"login":"jnm2","id":8040367,"avatar_url":"https://avatars.githubusercontent.com/u/8040367?v=3","gravatar_id":"","url":"https://api.github.com/users/jnm2","html_url":"https://github.com/jnm2","followers_url":"https://api.github.com/users/jnm2/followers","following_url":"https://api.github.com/users/jnm2/following{/other_user}","gists_url":"https://api.github.com/users/jnm2/gists{/gist_id}","starred_url":"https://api.github.com/users/jnm2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jnm2/subscriptions","organizations_url":"https://api.github.com/users/jnm2/orgs","repos_url":"https://api.github.com/users/jnm2/repos","events_url":"https://api.github.com/users/jnm2/events{/privacy}","received_events_url":"https://api.github.com/users/jnm2/received_events","type":"User","site_admin":false},"created_at":"2016-06-13T16:29:38Z","updated_at":"2016-06-13T16:29:38Z","body":"Out of curiosity- if a constructor takes a cancellation token, what would be the best way API-wise to disambiguate the scope of the cancellation? Applying just in the ctor vs applying to other actions in the class?"}}
{"actor":{"display_login":"jnm2"}}