-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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] Make ?? pattern-based for struct left-hand-side #4347
Comments
Why? In what situations would that be useful? Why do you think such situations happen often enough for this change to be worth it? |
Right now we cannot replace |
Why would you need a "better custom alternative" to |
QuickBooks SDK API for example is defined around HasValue which needs to be checked before reading the value. Sent from my iPhone
|
yes, but imagine tailoring the language to every api? that could quickly grind C# new features development to a halt. |
Already solved the problem but I don't see how to use null coalescing. I have lots of code that look like Not sure how to convert this to take advantage of current feature. Sent from my iPhone
|
The simplest approach depends on what you can modify. Above, I assume you are using the API as documented https://developer.intuit.com/docs/0100_accounting with XML messages over HTTP, in which case xsd.exe generates the following (shortened) type from https://developer.intuit.com/docs/@api/deki/files/54/bankingaccount_1.xsd: Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=4.6.81.0.
'
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="http://schema.intuit.com/platform/fdatafeed/bankingaccount/v1"), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://schema.intuit.com/platform/fdatafeed/bankingaccount/v1", IsNullable:=false)> _
Partial Public Class BankingAccount
Inherits Object
Implements System.ComponentModel.INotifyPropertyChanged
Private bankingAccountTypeField As System.Nullable(Of BankingAccountType)
Private bankingAccountTypeFieldSpecified As Boolean
Private postedDateField As System.Nullable(Of Date)
Private postedDateFieldSpecified As Boolean
Private availableBalanceAmountField As System.Nullable(Of Decimal)
Private availableBalanceAmountFieldSpecified As Boolean
Private interestTypeField As String
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=true)> _
Public Property bankingAccountType() As System.Nullable(Of BankingAccountType)
Get
Return Me.bankingAccountTypeField
End Get
Set
Me.bankingAccountTypeField = value
Me.RaisePropertyChanged("bankingAccountType")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property bankingAccountTypeSpecified() As Boolean
Get
Return Me.bankingAccountTypeFieldSpecified
End Get
Set
Me.bankingAccountTypeFieldSpecified = value
Me.RaisePropertyChanged("bankingAccountTypeSpecified")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=true)> _
Public Property postedDate() As System.Nullable(Of Date)
Get
Return Me.postedDateField
End Get
Set
Me.postedDateField = value
Me.RaisePropertyChanged("postedDate")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property postedDateSpecified() As Boolean
Get
Return Me.postedDateFieldSpecified
End Get
Set
Me.postedDateFieldSpecified = value
Me.RaisePropertyChanged("postedDateSpecified")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=true)> _
Public Property availableBalanceAmount() As System.Nullable(Of Decimal)
Get
Return Me.availableBalanceAmountField
End Get
Set
Me.availableBalanceAmountField = value
Me.RaisePropertyChanged("availableBalanceAmount")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property availableBalanceAmountSpecified() As Boolean
Get
Return Me.availableBalanceAmountFieldSpecified
End Get
Set
Me.availableBalanceAmountFieldSpecified = value
Me.RaisePropertyChanged("availableBalanceAmountSpecified")
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=true)> _
Public Property interestType() As String
Get
Return Me.interestTypeField
End Get
Set
Me.interestTypeField = value
Me.RaisePropertyChanged("interestType")
End Set
End Property
Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Sub RaisePropertyChanged(ByVal propertyName As String)
Dim propertyChanged As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
If (Not (propertyChanged) Is Nothing) Then
propertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0"), _
System.SerializableAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schema.intuit.com/platform/fdatafeed/bankingaccount/v1")> _
Public Enum BankingAccountType
'''<remarks/>
CHECKING
'''<remarks/>
SAVINGS
'''<remarks/>
MONEYMRKT
'''<remarks/>
RECURRINGDEPOSIT
'''<remarks/>
CD
'''<remarks/>
CASHMANAGEMENT
'''<remarks/>
OVERDRAFT
End Enum which supports null coalescing: Sub Foo(acc As BankingAccount)
Dim interestType = If(acc.interestType, "none")
End Sub The specifics you are after likely differ from that very example, but looking for a fix upstream in the chain to end up with a more usable API surface is quite generic. And in the case of a web service API and code generation, you can often achieve improvements without modifying the language and compiler. In your example, that would be in the definition of 'x' type and its properties. |
I am using the interop libraries, don't know how they were generated but your example is something I will look into. My question was more generic. In a typical case I want to check x for Nothing and if not Nothing check a x.method for valid value. It appears the "else" part is where my return goes (or I need to use Not). Unless I am misunderstanding how this works I guess invertIf will be a very popular refactoring. If x?.age>0 then Sent from my iPhone
|
This would make it work with a "Option" type value as well. |
We've decided not to do this. The |
Right now
??
can only be used with reference types orNullable T
value types. Could it be amended so it works with all structs that havebool HasValue
andT GetValueOrDefault
?The text was updated successfully, but these errors were encountered: