Skip to content

Def - alternative lambda definition or maybe local functions #221

@rskar-git

Description

@rskar-git

In a nutshell:

' Single line Function
Def Function <identifier> (<param-list>) [As <type>] <single-expression>

' Single line Sub
Def Sub <identifier> (<param-list>) <single-statement>

' Multi-line Function
Def Function <identifier> (<param-list>) [As <type>]
   <code-block>
End Function 

' Multi-line Sub
Def Sub <identifier> (<param-list>)
   <code-block>
End Sub 

The keyword Def is suggested on the basis of old BASIC (DEF FN) and Python. The Def has an identifier that is taken to be a Delegate that is ReadOnly. Perhaps some compiler optimizations can be made from that ReadOnly condition (e.g. https://docs.microsoft.com/en-us/dotnet/csharp/local-functions-vs-lambdas).

Example:

Imports System
Imports System.IO;

Class Example

Shared Sub Main()
    Dim contents  As string = GetText("C:\temp", "example.txt")
    Console.WriteLine($"Contents of the file:{vbCrLf}" + contents)
End Sub

Private Shared Function GetText(string path, string filename) As String 

    Def Function AppendPathSeparator(filepath As String) As String
        If (Not filepath.EndsWith("\")) Then filepath += "\"
        Return filepath
    End Function

    Dim sr = File.OpenText(AppendPathSeparator(path) + filename)
    Dim text = sr.ReadToEnd()
    return text

End Function

End Class

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions