【问题标题】:Memoise Function Compilation Error in LINQpadLINQpad 中的 Memoise 函数编译错误
【发布时间】:2014-08-09 05:38:33
【问题描述】:

以下内容无法在 LINQpad 中编译(语言=VB Program):

'    Hans-Christian Holm - Case study: Making use of functional programming techniques in .NET
'    [https://vimeo.com/97541187]
'    http://www.yr.no/

readonly Memos = new ConcurrentDictionary(of string, XElement)

function Memoise(f as Func(of XElement), key as string) as Func(of XElement)
    return function() Memos.GetOrAdd(key, function(k) f()) 'ERROR here!
end function

function ViewItem(temperature as integer) as XElement
    return <li><%= temperature %></li>
end function

function ViewList(temperatures as integer()) as XElement
    return <ul><%= temperatures.Select(addressof ViewItem) %></ul>
end function

function GetData() as integer()
    dim s as string = "Getting data..."
    s.Dump()
    return {2, 3}
end function

function ListComponent() as XElement
    dim data = GetData()
    return <div><%= ViewList(data) %></div>
end function

sub Main
    dim c = Memoise(addressof ListComponent, "my-component")
    c().Dump()
    c().Dump()
end sub

这是错误信息:

Overload resolution failed because no Public 'GetOrAdd' can be called with these arguments:
    'Public Function GetOrAdd(key As String, value As System.Xml.Linq.XElement) As System.Xml.Linq.XElement':
        Argument matching parameter 'value' cannot convert from 'VB$AnonymousDelegate_0(Of Object,XElement)' to 'XElement'.
    'Public Function GetOrAdd(key As String, valueFactory As System.Func(Of String,XElement)) As System.Xml.Linq.XElement':
        Argument matching parameter 'valueFactory' cannot convert from 'VB$AnonymousDelegate_0(Of Object,XElement)' to 'Func(Of String,XElement)'.

【问题讨论】:

    标签: vb.net functional-programming linqpad


    【解决方案1】:

    在 LinqPad 中选择 Edit > Preferences > Advanced > Compile VB queries in strict mode > True;将您的第一行更改为:

    readonly Memos as ConcurrentDictionary(of string, XElement) = new ConcurrentDictionary(of string, XElement)
    

    并在 Memoise() 函数中将 funcion(k) 更改为 function(k as string)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      相关资源
      最近更新 更多