Skip to content

Map a IsIn b to Boolean-returning b.Contains(a) instance or extension method call #298

@zspitz

Description

@zspitz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LDM Reviewed: No plansLDM has reviewed and this feature is unlikely to move forward in the foreseeable future

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions