Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB Recursive Lambda Sub does not compile

Tags:

vb.net

lambda

I've created the following recursive lambda expression that will not compile, giving the error

Type of 'OpenGlobal' cannot be inferred from an expression containing 'OpenGlobal'.

            Dim OpenGlobal = Sub(Catalog As String, Name As String)
                             If _GlobalComponents.Item(Catalog, Name) Is Nothing Then
                                 Dim G As New GlobalComponent
                                 G.Open(Catalog, Name)
                                 _GlobalComponents.Add(G)
                                 For Each gcp As GlobalComponentPart In G.Parts
                                     OpenGlobal(gcp.Catalog, gcp.GlobalComponentName)
                                 Next
                             End If
                         End Sub

Is what I'm trying to do possible?

like image 345
Kratz Avatar asked Sep 11 '25 04:09

Kratz


1 Answers

The problem is type inference. It can't figure out the type for your OpenGlobal variable because it depends on itself. If you set an explicit type, you might be okay:

 Dim OpenGlobal As Action(Of String, String) = '...

This simple test program works as expected:

Sub Main()
    Dim OpenGlobal As Action(Of Integer) = Sub(Remaining As Integer)
                                               If Remaining > 0 Then
                                                   Console.WriteLine(Remaining)
                                                   OpenGlobal(Remaining - 1)
                                               End If
                                           End Sub

    OpenGlobal(10)
    Console.WriteLine("Finished")
    Console.ReadKey(True)
End Sub
like image 149
Joel Coehoorn Avatar answered Sep 12 '25 20:09

Joel Coehoorn