类型初始值设定项的NullReferenceException

时间:2013-02-20 00:20:03

标签: f# mono monodevelop

我在下面的代码中使用F#类型和数据结构(我在Mac上使用Monodevelop,这只发生在交互式中):

type UnbalancedSet<'a> =
    | E
    | T of UnbalancedSet<'a> * 'a * UnbalancedSet<'a>

let rec insert x = function
    | E -> T(E, x, E)
    | T(a, y, b) as s -> 
        if x < y then T(insert x a, y, b)
        elif x > y then T(a, y, insert x b)
        else s

它适用于简单类型作为int浮点数和字符,但是当涉及到字符串或元组时,会出现以下错误:

let a = insert (3, 9) E;;

System.TypeInitializationException: An exception was thrown by the type initializer for UnbalancedSet`1 ---> System.NullReferenceException: Object reference not set to an instance of an object
  at FSI_0004+UnbalancedSet`1[System.Tuple`2[System.Int32,System.Int32]]..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at <StartupCode$FSI_0004>.$FSI_0004.main@ () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,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

我不明白发生了什么。我希望这个代码可以工作,因为类型是可比的。有线索吗?

1 个答案:

答案 0 :(得分:3)

这看起来像是一个MonoDevelop问题 - 我可以在VS2010的F#Interactive中运行你的示例代码:

> let a = insert (3, 9) E;;

val a : UnbalancedSet<int * int> = T (E,(3, 9),E)

除非其他人遇到同样的问题并在此处发布解决方案,否则您应该尝试将此帖子发布到MonoDevelop邮件列表和/或询问GIMPnet IRC上的#monodevelop频道。

http://monodevelop.com/index.php?title=Help_%26_Contact