-
Notifications
You must be signed in to change notification settings - Fork 66
Description
Examples:
Dim i = 5
Dim lst = New List(Of Integer) {1,2,3,4,5}
If i IsIn lst Then ...
'compiler maps to instance method
'If lst.Contains(i) Then ...
'even if LINQ Contains extension method is in scope, because of standard overload resolution rules
Dim s = "AB"
If s IsIn "ABCD" Then ...
'compiler maps to instance method
'If "ABCD".Contains(s) Then ...
'problem -- Contains is case-sensitive, and VB.NET is not; mentioned in #144
If i IsIn {1, 2, 3, 4, 5} Then ...
'compiler maps to BCL extension method
'If {1, 2, 3, 4, 5}.Contains(5) Then ...
Module InExtensions
'Dictionary types
<Extension> Public Function Contains(Of TKey, TValue)(dictionary As Dictionary(Of TKey, TValue), key As TKey) As Boolean
Return dictionary.ContainsKey(key)
End Function
End Module
Dim dict = New Dictionary(Of String, Integer) {
{"one", 1},
{"two", 2},
{"three", 3}
}
Dim key = "three"
If key IsIn dict Then ...
'compiler maps to above defined extension method, which in turn calls .ContainsKey
'If dict.Contains(key) Then ...
Related proposals
This syntax has been the subject of other proposals:
Proposal #62 -- as leveraging an overloadable operator, and not limited to returning Booleans
This limits the value of this feature exclusively to unsealed types, or types for which the source code can be modified. Also, returning something other than Boolean violates both the plain English meaning, and the commonly accepted meanings in VB.NET and SQL.
Proposal #229 -- Special-case for multitype-checking (#93) range (#25) and enum (#228)
RE: multitype checking -- The syntax in that proposal is specific to checking whether a type matches one of the types (as opposed to checking if it matches against all of the types). (I myself would prefer a more flexible syntax that would allow for both). That scenario would not be covered by this proposal.
RE: range -- If ranges would compile down to a Range object, which would have a Contains instance method , it would also be covered by this proposal.
RE: enum -- I understand C# now supports enum as a constraint for generic methods; if VB.NET would support the same, this scenario could also be covered by this proposal, using a .Contains extension method with an enum constraint.