Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# System.TypeLoadException

by this code

module ObjectRe =
  type BM = A | N
  type Object = {
    Name: string
    Pattern: BM array
  }

  let patterns = [|{Name = "Pro"; Pattern = [|A;N;N;A|]} |]

I always get this error message

System.TypeLoadException: Could not load type 'FSI_0007+ObjectRe+Object[]' from assembly 'FSI-ASSEMBLY, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in <94fd79a3b7144c54b4cb162b50fc7761>:0 
Stopped due to error

Has anyone an idea? I use Visual Studio Code on Mac.

Thanks

like image 935
kudi Avatar asked Sep 20 '16 09:09

kudi


1 Answers

I can also reproduce this on macOS 10.11, mono 4.6.

The minimal reproduction is :

module X =
    type Y = Y
    let l = [| Y |]

With a workaround :

module X =
    type Y = Y
    let g = List.toArray [ Y ]

So in your case replace both the inner and the outer [| |] declarations.

Why this happens only in VS Code/fsharpi but not in Xamarin?

If you fire up activity monitor you will see that VS Code/fsharpi uses the /Library/Frameworks/Mono.framework/Versions/4.6.0/lib/mono/4.5/fsi.exe binary, but Xamarin Studio uses the FCS Xamarin Studio.app/Contents/Resources/lib/monodevelop/AddIns/BackendBindings/MonoDevelop.FSharpInteractive.Service.exe You can check also the version on the first message from the interactive (F# Interactive for F# 4.1, those might be different).

Finally, i believe this is a bug on mono + dynamic assemblies, triggered by the code emitted by the fsi. Compiling this code to an exe works fine on all platforms (and if you check the generated IL seems pretty normal).

like image 145
krontogiannis Avatar answered Nov 12 '22 16:11

krontogiannis