dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
288 stars 65 forks source link

[Proposal] Async Property #99

Open Nukepayload2 opened 7 years ago

Nukepayload2 commented 7 years ago

Summary

Asynchronous property is used to read or write data asynchronously.

Motivation

When we are developing App to App communication features, if we want to get or set values, we usually write asynchronous methods.

' 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.

' 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

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

' 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# 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

Unlike properties with index(es), reflection can't get the PropertyInfo of asynchronous properties.

rrvenki commented 4 years ago

Many times I end up using a Subroutine/Function in the Setter of a property instead of inotifypropertychanged event. It would be better if its an event, but... I wish to make these Subroutine/Function Async/Await instead of new thread. But VB does not allow Await in Property declarations.

For example i'll fire this Sub/Func in Async mode to perform face detection and check permissions of the individuals from DB, etc. ONLY when "the count of faces detected in the camera changes". This functionality could be network, DB, and processing intensive which should be Async preferably.

@Nukepayload2 @AnthonyDGreen @KathleenDollard Can we expect this feature soon please? Or is there any efficient workaround other than creating a new thread function?

vbcodec commented 4 years ago

do you still believe in new features to be added to this language ? IMO, current status is 'live & locked'

rrvenki commented 4 years ago

do you still believe in new features to be added to this language ? IMO, current status is 'live & locked'

"this language" is VB I assume. -> Microsoft (Mr Gates Sir) might have created the language and even lived its life. The new blood in Microsoft may like C# more and even ignore VB. All doesn't matter much now. The number of passionate people who still use VB matters. I see even now (new generation) people see value in VB. In my opinion there are C guys and Rest of C guys. C guys like { and ; which others may not. Fine...

If you have not heard, a Chinese company has started enhancing VB and provides an independent build. An American company has started Xojo (RealBasic) which works in multiple platforms.

It is only a matter of time when we people switch ships. But remember VB still will live irrespective of Microsoft.

So please don't say no new features will be added to this language. This language is very much alive in other places too where features are being added.