Adding StructLayout attribute to F # type with implicit constructor

I have:

type Package = abstract member Date : int abstract member Save : unit -> unit [<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>] type Instant(date : int, value : int) = let mutable _date = date let mutable _value = value member X.Value : int = _value interface Package with member X.Date : int = _date member X.Save() = ... 

but getting error: Only structures and classes without implicit constructors can be assigned the StructLayout attribute

so I understand that it should be something similar:

 type Instant = struct val Date : byte array ... 

But in this way I lost my interface. In C #, for example, adding type:StructLayout possible for this type of class (I think). How do I reorganize my code to avoid this error?

+4
source share
1 answer

As the error message says, StructLayout should work with explicit constructors :

 type Package = abstract member Date : int abstract member Save : unit -> unit [<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>] type Instant = val mutable Date: int val mutable Value: int new(date, value) = {Date = date; Value = value} interface Package with member x.Date = x.Date member x.Save() = x.Value |> ignore 

If you are having problems with FieldOffset , check out the code examples here .

+5
source

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


All Articles