【问题标题】:a generic version of an F# function is causing invalid IL?F# 函数的通用版本导致无效的 IL?
【发布时间】:2011-01-02 05:14:55
【问题描述】:

所以我写了一个小二分搜索函数(不是我需要,只是因为我可以),例如,当我使它特定于字符串或整数时,它运行良好。当我尝试使用泛型类型签名时,我开始收到异常。

使用泛型类型的函数的副本'a

let search (needle : 'a) (haystack: 'a array) : int = 
    let length = Array.length haystack

    if Array.length haystack <= 0
    then -1
    else
        let bottom = 0
        let top = Array.length haystack - 1

        let rec search' (needle : 'a) (haystack: 'a array) (bottom:int) (top:int) : int = 
            if bottom = top
            then if needle = haystack.[top] then top else -1
            else
                let middle = (uint32 top + uint32 bottom) >>> 1 |> int  // avoid an overflow

                if needle <= haystack.[middle]
                then search' needle haystack bottom middle
                else search' needle haystack (middle+1) top

        search' needle haystack bottom top 

当它被调用时,我得到以下信息:

System.InvalidProgramException: Invalid IL code in FSI_0019:search'@921-13<a> (a,a[],int,int): IL_0000: br        IL_0005
  at FSI_0019.search[String] (System.String needle, System.String[] haystack) [0x00000] in <filename unknown>:0 
  at <StartupCode$FSI_0023>.$FSI_0023.main@ () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
Stopped due to error

我做错了吗? (同样,当我使用 stringint 而不是 'a 时,一切正常......)

编辑:

我已经编译了 Mono 2.9 左右,然后这个函数在 FSI 中工作。现在,等待 Debian 和 Ubuntu 升级... :D

【问题讨论】:

  • 您使用的是哪个版本的 Mono? Mono 2.8 中修复了一些外观相似的错误...
  • 版本 2.6.7...(显然,现在即使 Ubuntu 11.04 也包含 2.6.7。我在 10.10 中。)嗯。

标签: f# mono generics il


【解决方案1】:

单声道错误。 之前,我的代码在 fsi 上不起作用,但可以编译。

编辑:已确认该代码不适用于 fsi,但适用于 fsc 编译。

【讨论】:

  • 嗯,是的,通过运行菜单从 MonoDevelop 运行必须使用 fsc,因为这样可以...
  • 在此处记录错误:mono-project.com/Bugs。值得一提的是,该错误位于“Reflection.Emit”(fsi 使用的路径)中,而不是 Mono 编译器本身。
  • 嗯。我明天再试一次,但现在 bugzilla.novell.com 不存在。
【解决方案2】:

这对我有用(在 .NET 4.0 上运行 FSI)。也许这是一个 Mono 错误?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多