开发者

VB Recursive Lambda Sub does not compile

开发者 https://www.devze.com 2023-02-24 11:44 出处:网络
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\'.

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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消