Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using custom Marshaller in F#

I've been toying with marshalling in F# recently, and to my surprise I ended up with error FS0980: Custom marshallers cannot be specified in F# code. Consider using a C# helper function.

Only information regarding the topic, that I managed to find, is F# 4.0 Spec (page 300)

When applied to a parameter or return type, specifies the marshalling attribute for a CLI P/Invoke stub declaration. This attribute may be used in both F# and imported assemblies. However, F# does not support the specification of "custom" marshallers.

I'm mostly interested in reasons behind lack of custom marshallers support, as I can "live with" introducing C# assembly to project.

Can anyone clarify on this?

EDIT:

Sample code as requested

[<StructLayout(LayoutKind.Sequential, Pack=1)>]
type BASIC_STRUCT =
  struct
    [<MarshalAsAttribute(UnmanagedType.CustomMarshaler,MarshalTypeRef=typedefof<DateTimeMarshaller>)>]
    val timeVar:DWORD
  end

from what i noticed just setting UnmanagedType.CustomMarshaler is enough to cause compile time exception

like image 743
Paweł Paluch Avatar asked Mar 20 '16 21:03

Paweł Paluch


1 Answers

Looking at the compiler code, it seems the F# team couldn't really make up their mind about this:

| 0x2C -> 
    error(Error(FSComp.SR.ilCustomMarshallersCannotBeUsedInFSharp(),m))
    (* ILNativeType.Custom of bytes * string * string * bytes (* GUID,nativeTypeName,custMarshallerName,cookieString *) *)
    //ILNativeType.Error  

There's no history around that piece of code on github. My gut feeling is that the explanation would be something along the lines of "uncommon use case with a cheap workaround". Very likely it's been that way from the beginning, since F# 2.0 spec also has the line you quoted.

Try raising an issue on github about it. Maybe someone there can dig up something in Microsoft systems.

like image 109
scrwtp Avatar answered Nov 14 '22 19:11

scrwtp