f# - F# 类型约束错误尝试编写类型映射
问题描述
我正在尝试在 C# 库之上编写 API。C# 代码有一个从类型到值的字典,我想用不可变的数据结构来表示。
但是,我无法正确编写类型约束:
open System
// System.Type cannot be used a map key
// TODO: Is this enough data to form a key?
type ComparableType =
{
AssemblyQualifiedName : string
}
type TypeMap<'t> =
private
| TypeMap of Map<ComparableType, 't>
module TypeMap =
let private innerMap =
function
| TypeMap m -> m
let empty () =
TypeMap Map.empty
// Here is the problem line!
let add<'t, 'v when 'v :> 't> (v : 'v) (m : TypeMap<'t>) =
let t = typeof<'v>
let k =
{
AssemblyQualifiedName = t.AssemblyQualifiedName
}
m
|> innerMap
|> Map.add k (v :> 't)
|> TypeMap
错误:
无效约束:用于约束的类型是密封的,这意味着约束只能由最多一种解决方案满足
解决方案
我不认为 F# 可以处理这样的通用约束。此外,它不会让您对未确定的类型进行强制处理。你最好的选择是使用box v
而不是(v :> 't)
像这样:
type ComparableType =
{
AssemblyQualifiedName : string
}
type TypeMap =
private
| TypeMap of Map<ComparableType, obj>
module TypeMap =
let private innerMap =
function
| TypeMap m -> m
let empty () =
TypeMap Map.empty
let inline add (v : 'v) (m : TypeMap) =
let t = typeof< 'v>
let k =
{
AssemblyQualifiedName = t.AssemblyQualifiedName
}
m
|> innerMap
|> Map.add k (box v)
|> TypeMap
let inline get< ^v> (m : TypeMap) : ^v =
let t = typeof<'v>
let k =
{
AssemblyQualifiedName = t.AssemblyQualifiedName
}
(innerMap m).[k]
|> unbox
推荐阅读
- firebase - Firebase 身份验证服务
- angular - 在 Angular / Typescript 的 *ngFor 中添加 if else 子句
- javascript - 如何使用javascript将jsonresult绑定到mvc中的标签文本
- java - 用Eclipse在java文件中找到刚刚写的代码(即高亮新部分的代码)
- r - 按年份将数据框转换为存在缺席(包括缺失年份)
- r - 平均组学/生物统计学中的重复数据
- c# - c# 可观察对象冷热
- php - 如何在不使用 PHP 打开该页面的情况下安排电子邮件
- qlikview - 使用循环的 Qliksense REST 偏移分页
- xamarin - 应用程序在 UWP 中的 Windows 应用程序认证工具包中的验证应用程序中没有进展?