LINQpad中的Memoise函数编译错误

时间:2014-08-09 05:38:33

标签: vb.net functional-programming linqpad

以下内容无法在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)'.

1 个答案:

答案 0 :(得分:1)

在LinqPad中选择编辑&gt;偏好&gt;高级&gt; <严格模式下编译VB查询>真;将您的第一行更改为:

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

并在funcion(k)功能中将function(k as string)更改为Memoise()