如果委托类型是内部的,我在为F#中的属性getter编译lambda表达式时遇到一些麻烦.这就是函数的样子:
// if I omit the 'internal' here everything works as expected
module internal ReflectionHelpers =
open System
open System.Linq.Expressions
open System.Reflection
// it makes no difference if this delegate type is defined outside
// of the module and marked as 'internal'
type GetterFunc<'T> = delegate of 'T -> obj
/// Build a getter expression function for the
/// specified PropertyInfo
let getGetter<'a> (p : PropertyInfo) =
let inst = Expression.Parameter(p.DeclaringType, "i")
let prop = Expression.Property(inst, p)
let …Run Code Online (Sandbox Code Playgroud) 我在F#中编写了一些小的字符串解析函数 - 以便更好地了解F#并了解如何使用它来解决此类任务.我尝试遍历字符串并通过递归搜索特定字符.
逻辑确实有效,但在我看来,生成的版本构建的IL代码(启用了优化)确实看起来很奇怪.所以我想有更好的方法在F#中以高效的方式编写这些东西.
这是解析函数的一部分:
let eatTag (input : string) index =
let len = input.Length
let nothing = 0, null, TagType.Open
// more functions used in the same way
// ...
let rec findName i =
if i >= len then nothing
else
let chr = input.[i]
if isWhitespace chr then
findName (i+1)
elif chr = '/' then
getName (i+1) (i+1) true
else getName (i+1) i false
let rec findStart i =
if i >= len then nothing
elif …Run Code Online (Sandbox Code Playgroud)