Create new record type inline by extending existing record with new member

我的梦境 提交于 2019-12-10 23:32:45

问题


After asking this question and trying out this suggested solution, I have the following generic entity type:

type Entity<'a> = {
    Id : Guid
    Data : 'a
}

Now, before I expose this in an API, I want to flatten it, so that the Id and the properties in Data can be serialized next to each-other. I tried this, but it doesn't compile:

let flatten<'a> (v : Entity<'a>) = { v.Data with Id = v.Id }

What I had hoped to achieve, was to return a record type such that if 'a is e.g. { foo : string; bar : int }, then flatten { Id = someGuid; Data = { foo : "baz"; bar : 42 } } would return { Id = someGuid; foo = "baz"; bar = 42 } (or equivalent).

Is there a way to accomplish this, or am I trying to make the type system too dynamic?

来源:https://stackoverflow.com/questions/39306148/create-new-record-type-inline-by-extending-existing-record-with-new-member

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!