Does the generic version of F # function cause invalid IL?

So, I wrote a little binary search function (not what I need, but only because I can), and when I make it specific to strings or integers, for example, it works well. When I try to use a generic type signature, I start getting exceptions.

Copy function using generic type '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 

When this is called up, I get the following:

 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 

Am I doing something wrong? (Again, when I use string or int instead of 'a , everything works ...)

Edit:

I compiled Mono 2.9 or so, and this function then works in FSI. Now, to wait for the update of Debian and Ubuntu ...: D

+4
source share
2 answers

Mono mistake. I used to have code that does not work on fsi but is compiled.

EDIT: he confirmed that this code does not work on fsi , but works with compiled fsc .

+2
source

This works for me (running FSI on .NET 4.0). Perhaps this is a Mono error?

+2
source

Source: https://habr.com/ru/post/1333972/


All Articles