[Proposal] Async Property
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
## Summary
[summary]: #summary
Asynchronous property is used to read or write data asynchronously.
## Motivation
[motivation]: #motivation
When we are developing App to App communication features, if we want to get or set values, we usually write asynchronous methods.
``` VB.NET
' Define
Async Function GetSomeValueAsync() As Task(Of Integer)
Await server.ReadInt32("SomeValue")
End Function
Async Function SetSomeValueAsync(value As Integer) As Task
Await server.WriteInt32("SomeValue", value)
End Function
' Use
Dim someValue = Await GetSomeValueAsync
Await SetSomeValueAsync(someValue)
```
This scenario is unable to benefit from the NameOf keyword. Because "SomeValue" is not a member name or a type name.
When we want to rename a property ("SomeValue" in this example), we should rename two methods (get and set) and one or two string constants. It's possible to forget to rename a method or a string constant.
```VB.NET
' Rename SomeValue to StartIntex
Async Function GetStartIndexAsync() As Task(Of Integer)
Await server.ReadInt32("StartIndex")
End Function
Async Function SetStartIndexAsync(value As Integer) As Task
' Oops. I forget to change this string constant.
' A runtime exception is coming.
Await server.WriteInt32("SomeValue", value)
End Function
```
So, we need a new syntax to read or write values asynchronously.
## Detailed design
[design]: #detailed-design
The compiler generates asynchronous get (Get$propName$Async) and set (Set$propName$Async) methods for asynchronous properties, and transform asynchronous property reads or writes to asynchronous method calls.
Example code:
__VB__
```VB.NET
' Custom asynchronous property:
Public Async Property LongCount As Task(Of Long)
Async Get
Return Await server.GetInt64Async(NameOf(LongCount)) ' Benefit from the NameOf keyword .
End Get
Async Set(value As Long)
Await server.SetInt64Async(NameOf(LongCount), value) ' Can rename this property with Ctrl+. in visual studio directly.
End Set
End Property
' Reflection view:
' MethodInfo.IsSpecialName = True
Function get_LongCountAsync() As Task(Of Long)
' MethodInfo.IsSpecialName = True
Function set_LongCountAsync(value As Long) As Task
' Usage:
Dim longCount = Await LongCount
Await LongCount = longCount
' Decompiled code:
Dim longCount = Await get_LongCountAsync
Await set_LongCountAsync(longCount)
```
__C#__
```c#
// c# usage:
var longCount = await get_LongCountAsync();
await set_LongCountAsync(longCount);
```
For late bindings, when reading an async property, the LateCall method search for normal properties and methods first. If it's not found, then try to find properties with index(es) and async properties.
When writing an async property, the LateCall method will handle ref-return first. If it's not ref-return, try to get the set method through the AsyncPropertyNameAttribute.
## Drawbacks
[drawbacks]: #drawbacks
Unlike properties with index(es), reflection can't get the PropertyInfo of asynchronous properties.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.