Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Extension Property? #73

Open
xieguigang opened this issue May 7, 2017 · 4 comments
Open

[Proposal] Extension Property? #73

xieguigang opened this issue May 7, 2017 · 4 comments
Labels

Comments

@xieguigang
Copy link

As all we knowns that since VB6, the Property Procedures in VisualBasic is consist with a paired method and function which have name prefix Get and Set. The vb6 compiler can automatic properties this paired method as Class property.

Sometime we just want a light weight class type without too many function and property, and using the extension method can simply extend this light weight class type and add more function based on our different demands. But as so far, we don't have the extension property yet, but we can doing something currently like:

' ReadOnly Property
<Extension> Public Sub SetLast(list As List(Of Foo), value As Foo)
    ' blablabla
    list(-1) = value
End Sub

' WriteOnly Property
<Extension> Public Function GetLast(list As List(Of Foo)) As Foo
    ' blabla
    Return list(-1)
End Function 

So that I think why don't we just enable using this extension method way to add a extension property as the VB6 it does?

The compiler match this two extension function their name with rule: Set<Name> and Get<Name>, so that we can paired these two extension method as an extension property.

Then we use such extension method as in sciBASIC it does: https://github.com/xieguigang/GCModeller/blob/7f44b456e1df40b450614a21769126c4f23c35f8/src/interops/localblast/LocalBLAST/LocalBLAST/LocalBLAST/Application/COG/Whog/Category.vb#L110

Dim list As New List(Of NamedValue)

' blablabla
list.Last = New NamedValue With {
    .name = list.Last.name,
    .text = list.Last.text & " " & Trim(nid.Value)
}

For the parameter property, that we can

' ReadOnly Property
<Extension> Public Sub SetCompute(list As List(Of Foo), Name$, value As Foo)
    ' blablabla
End Sub

' WriteOnly Property
<Extension> Public Function GetCompute(list As List(Of Foo), Name$) As Foo
    ' blabla
End Function 

' using extension property
Data.Compute(Name) = Data2.Compute(Name)
@markhurd
Copy link

And note VB Axis Queries already provides a Property Extension for .Value: https://docs.microsoft.com/en-us/dotnet/articles/visual-basic/language-reference/xml-axis/xml-value-property

@xieguigang
Copy link
Author

Hi, @markhurd,

Using XML value property is kind of like using Dictionary(Of String, Object), and using Dictionary is more convenient and faster than XML value property.

Using Dictionary as the extension property example:

Dim o As New Dictionary(Of String, Object)

o!ABC = New Dictionary(Of String, Double) From {
    {"ABC", 123.0#},
    {"BBB", 666.0#}
}
o!Date = #2017-5-11 11:11:11#

Call println("ABC=%s,\n\nDate=%s\n", GetObjectJson(o!ABC, o!ABC.GetType), o!Date)

qq 20170511193946

But the problem is that

  • both using XML value property or Dictionary way as the extension property just let us store/read the value, but can not enable us using the custom procedure like the property.
  • And of course, both of these XML value property and Dictionary didn't have well supports on IntelliSense, but the extension method it does.

@AnthonyDGreen
Copy link
Contributor

@markhurd you've discovered the secret! We've talked for years about defining extension properties using the equivalent syntax we use for methods given that VB supports parameterized properties already. They wouldn't be generic but they'd be something.

@AnthonyDGreen
Copy link
Contributor

We're working on an "Extension everything" proposal which would let you define more than just methods and properties as extensions. It's a more general feature which we'd prefer over a special case. That said, since VB supports parameterized properties, we would consider a prototype demonstrating extending the existing extension method syntax to support parameterized properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants