Constraining type definition to enable Fleece (de-)serialization

Say I have a record type…

type Message<^a ...> =
    { id: Guid
      causation: Guid
      correlation: Guid
      name: string
      body: ^a }

I believe I need to use some duck typing to constrain the generic to implement OfJson and ToJson but I am struggling with the syntax.

Right now, I am using…

type Message< ^a when ^a : (static member OfJson: JsonValue -> ^a ParseResult)
                                 and ^a : (static member ToJson : ^a -> JsonValue)>

but when I define Message.JsonObjCode as

    static member JsonObjCodec = ...

I get an error ‘The code is not sufficiently generic. The type variable ^a (…) could not be generalized because it would escape its scope.’

Hmm, what am I missing?

I think you’re missing the inline keyword. Try “member inline”. See and

This is old but still for the record, here’s the answer.

Never declare the same parameter at type level declaration and in static member with constraints.Just use a different parameter name in the type declaration, without any constraint.

It’s probably not necessary to declare the method inline but I’m not sure as I can’t see its body.

1 Like

Thank you. Old but relevant and helpful!