genericdeveloper
genericdeveloper

Reputation: 109

Syntax for generic types parameterized by generic records

Suppose I have defined the generic record

type GenericType1<'T> = {MyField : 'T}

and I want to define another generic type parametrized by this type. Following my C# nose, I would write something like this:

type GenericType2<'T, GenericType1<'T> > = class end

Unfortunately, this does not work, nor does this:

type GenericType2<'T, 'S when 'S :> GenericType1<'T> > = class end

because GenericType1, being a record, is sealed and the compiler won't accept the subtype constraint. If GenericType1 is a class, however, the above does work.

So, my question is: what syntax should I use to define GenericType2 which is parameterized by the record GenericType1?

Upvotes: 1

Views: 678

Answers (1)

Daniel
Daniel

Reputation: 47904

As the error says, records are sealed, so what exactly would such a subtype constraint mean? You can do the following without an additional type arg.

type GenericType2<'T>() = 
    member val MyRecord = {MyField=Unchecked.defaultof<'T>}

Does that cover your use case?

Upvotes: 1

Related Questions