Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?"

like image 385
mamcx Avatar asked Jun 13 '15 14:06

mamcx


1 Answers

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 } |]}
like image 62
Tomas Petricek Avatar answered Sep 28 '22 06:09

Tomas Petricek