I was playing with F# types and datastructures in the following code (I’m using Monodevelop on a Mac and this only happens in the Interactive):
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
It works great with simple types as ints floats and chars but when it comes to strings or tuples it gives the following error:
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
I don’t understand what’s going on. I expected this code to work since the type is comparable. Any clues?
This looks like a MonoDevelop issue — I’m able to run your example code just fine in F# Interactive in VS2010:
Unless anyone else has run into this same issue and posts the solution here, you should try posting this to the MonoDevelop mailing list and/or asking the #monodevelop channel on GIMPnet IRC.
http://monodevelop.com/index.php?title=Help_%26_Contact