Skip to content

[Proposal] "In" keyword relationship operator #62

@xieguigang

Description

@xieguigang

Generally, the word In make sense for these situations:

  • Dictionary has the Key?
  • Collection contains the element?
  • Number in a specific range?
  • File exists in a directory?
  • Resource exists in a specific source?

So that by introduce the existed In keyword As a new operator, then we can makes the vb code more simple and human natural Language:

Public Class TableType

    Dim table As New Dictionary(Of String, Integer)

    Public Shared Operator In(key$, table As TableType) As Boolean
        Return table.table.ContainsKey(key) 
    End Operator

End Class

Dim c As New TableType
Dim x$

If x In c Then
End If
Public Class CollectionType

    Dim list As New List(Of Integer)

    Public Shared Operator In(x%, list As CollectionType) As Boolean
        Return list.list.IndexOf(x) > -1
    End Operator

End Class

Dim c As New CollectionType
Dim x%

If x In c Then
End If
Public Class IntRange
    Dim range%()

    Public Shared Widenning Operator CType(l%()) As IntRange
        Return New IntRange With {
            .range = l
        }
    End Operator

    Public Shared Operator In(x%, r As IntRange) As Boolean
        Return Array.IndexOf(r.Range, x) > -1
    End Operator
End Class

' If [1: 10] means integer range 1 to 10, then we can
Dim x%

If x In [1:10] Then
End If 

And combine this new In keyword with the Linux bash file search API syntax in VisualBasic language:

https://github.com/xieguigang/GCModeller/blob/master/src/GCModeller/CLI_tools/eggHTS/CLI/2.%20DEP.vb#L196

to makes the VB style in more Natural Language

Public Class Directory
    Dim DIR$

    Public Shared Widening Operator CType(DIR$) As Directory
        Return New Directory With { 
            .DIR =DIR
        }
    End Operator

    Public Shared Operator In(file$, DIR As Directory) As Boolean
        Return (ls -r <= DIR).IndexOf(file.BaseName) > -1
    End Operator
End Class

' In another style
Public Class Directory
    Dim files As CollectionType(Of String)

    Public Shared Widening Operator CType(DIR$) As Directory
        Return New Directory With { 
            .files = (ls -l -r -"*.*" <= DIR)
        }
    End Operator

    Public Shared Operator In(file$, DIR As Directory) As Boolean
        Return file$ In DIR.files
    End Operator
End Class

Dim file$
Dim DIR As Directory = "C:\test"

If file In DIR Then
End If

If file$ In (ls -l -r -"*.xml" <= "D:\data\") Then
End If

' or even more simple
Public Class File
    Dim file$

    Public Shared Operator In (file As File, DIR$) As Boolean
        With file
            Return (ls -l -r - .File.ExtensionName <= DIR).IndexOf(.File) > -1
        End With
    End Operator
End Class

Dim file As File

If file In "C:\test\" Then
End If

Compared with the List.IndexOf, Dictionary.ContainsKey, In keyword operator just using 2 character in our code, simple and make sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions