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

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

Open
zspitz opened this issue May 8, 2018 · 3 comments
Labels
LDM Reviewed: No plans LDM has reviewed and this feature is unlikely to move forward in the foreseeable future

Comments

@zspitz
Copy link

zspitz commented May 8, 2018

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.

@zspitz zspitz changed the title Map a In b to Boolean-returning b.Contains(a) instance or extension method call Map a IsIn b to Boolean-returning b.Contains(a) instance or extension method call Jul 29, 2018
@zspitz
Copy link
Author

zspitz commented Jul 29, 2018

I had originally suggested using In for this, but on further consideration, perhaps it's better to use IsIn., which explicitly suggests a Boolean operator. Consider:

Dim result = a In b

could be construed to mean the a which is in b, and should return the object referred to by a if b indeed Contains a. This ambiguity is evident in other similar proposals (e.g. here).

@KathleenDollard KathleenDollard added the LDM Reviewed: No plans LDM has reviewed and this feature is unlikely to move forward in the foreseeable future label Oct 31, 2018
@KathleenDollard
Copy link
Contributor

This increases the surface area of VB to save a few characters.

Also, if it is revived, consider impact of overloading, which may result in surprises.

No Plans

@zspitz
Copy link
Author

zspitz commented Oct 31, 2018

@KathleenDollard

This increases the surface area of VB to save a few characters.

That goal could be accomplished with a set of extension methods. Rather, the goal here is a closer-to-natural-language syntax -- when describing the relationship of a is in b / b contains a, natural-language syntax is more frequently Joe is in the car, or Joe is in the group, rather than the car / group contains Joe.

impact of overloading, which may result in surprises

Could you elaborate on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LDM Reviewed: No plans LDM has reviewed and this feature is unlikely to move forward in the foreseeable future
Projects
None yet
Development

No branches or pull requests

2 participants