简体   繁体   English

如何在记录中声明一个具有F#中通用记录列表的成员

[英]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 我想创建任何类型的记录列表,并传递给序列化为json

PD: More info about the issue. PD:有关该问题的更多信息。

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?" 因此,我需要使用通用签名标记一切,因此可以更一般地说:“我可以在这里有任何类型的记录吗?”

You need to make the Table type generic too: 您还需要使Table类型通用:

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: 并且因为您的两个记录具有相同的字段,您可以使用Cpu.name明确表示您正在创建具有CPU值的表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM