How declare in a record a member with generic list of records in F#

929 views Asked by At

I'm stuck in how pass a generic list of records around. I wanna do this:

type TabularData<'T>= array<'T>
type Table = {title:string; data:TabularData<'T>} //This of course not work

type Cpu = 
  { name:string; load:int; }

type Memory = 
  { name:string; load:int; }

//F# not let me pass CPU or Memory

I wanna create any list of records of any type, and pass it around to serialize to json

P.D: More info about the issue.

I have forgott to add the main problem. Using generics it spread vast to the rest of the functions. So I need to tag EVERYTHING with a generic signature, so is possible to be more general and say: "I can have any kind of records here?"

1

There are 1 answers

0
Tomas Petricek On

You need to make the Table type generic too:

type Table<'T> = {title:string; data:TabularData<'T>} 

And because your two records have the same fields, you can use Cpu.name to explicitly say that you are creating a table with CPU values:

{ title = "hi"; data = [| { Cpu.name = "test"; load = 1 } |]}