gpt4 book ai didi

vb.net - VB递归Lambda Sub无法编译

转载 作者:行者123 更新时间:2023-12-02 09:33:25 27 4
gpt4 key购买 nike

我创建了以下递归 lambda 表达式,该表达式无法编译,并给出错误

无法从包含“OpenGlobal”的表达式推断出“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

我想做的事情可能吗?

最佳答案

问题在于类型推断。它无法确定 OpenGlobal 变量的类型,因为它取决于自身。如果您设置显式类型,则可能没问题:

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

这个简单的测试程序按预期工作:

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

关于vb.net - VB递归Lambda Sub无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com