- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在 F# 中编写泛型函数,在其中指定类型“T”,但将“T”限制为类型“T2*”T3 的元组?
以下是我尝试过的一些只会产生编译器错误的事情:
let fun1<'T when 'T :> 'T2*'T3> () = …
let fun2<'T, 'T2, 'T3 when 'T :> 'T2*'T3> () = …
(在 fun2 中,“T2/”T3 最好由“T1”推断。)
我想这样做的原因是我正在编写一个带有内存数据库的应用程序,并且我正在尝试编写一个通用函数来提取与指定元组形式的数据库实体关联的数据.我有一个可行的解决方案,但有一些缺点:
let workingExtractEntity<'T> (id : EntityID) (db: Database) : EntityID*'T =
let t = db.GetEntityComponent id typeof<'T>
(id, t)
以及具有更多类型参数的类似函数:
let workingExtractEntity2<'T1, 'T2> (id : EntityID) (db: Database) : EntityID*'T1*'T2 = …
这种方法的主要问题是每次使用该函数时,您都必须手动输入通用参数并对其进行排序。我更希望能够指定单个元组类型,并结合常用元组类型的类型别名,如下所示:
//Obviously won’t compile, but illustrates what I would like to do.
let extractEntity2<'T when 'T :> EntityID*'T2*'T3> id db : 'T =
let t2 = db.GetEntityComponent id typeof<'T2>
let t3 = db.GetEntityComponent id typeof<'T3>
(id, t2, t3)
type Player = EntityID*Position*Health
let extractPlayer id db : Player = extractEntity2<Player> id db
请注意,数据库中没有存储 Player 类型的对象,而只有构成 Player 的组件(即 Position 和 Health)存储在那里。 (任何具有与其关联的位置和健康的实体都可以表示为玩家。)
有什么办法可以达到这样的目的吗?
最佳答案
我做了一些研究,我想我已经找到了我自己问题的答案:
不幸的是,我想做的事情在 F# 中似乎是不可能的,并且很好地总结了原因 here由用户 Gus:
I'm afraid there is no way to add a subtype constraint to a generictype parameter based on another one in F#. They're always assumed tobe equal, see the spec New constraints of the form type :> 'b aresolved again as type = 'b.
这种限制在 C# 中不存在,这有时会使互操作很麻烦。有一个 issue在 F# 语言建议 github 页面上解决它。
换句话说,你不能这样写函数:
let foo<'T, 'U when 'T :> 'U> () = …
这意味着你也不能这样做:
let foo2<'T, 'T2, 'T3 when 'T :> Tuple<'T2, 'T3>> () = …
这基本上是我想要的,除了额外的类型推断,所以不用输入整个:
let callFoo2 = foo2<int*string, int, string> ()
你可以这样写,然后从 'T1 推断出 'T2 和 'T3:
let callFoo2Differently = foo2<int*string> ()
关于generics - 是否可以在 ‘T is constrained to be a tuple of type ' T2*'T3 处编写一个 genericFunction<'T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65149034/
在 Swift 中,我们可以对序列等通用项编写扩展: extension Sequence where Iterator.Element : ObservableType { } 这将保证扩展仅适用于
刚刚从视频教程中注意到Laravel有这个foreignId-constrained。所以我尝试将我的代码从 method.1 更改为 method.2,表已迁移,但是当我尝试 db:seed 时,默
在“project.project”模型中,我编写了一个函数来验证“开始日期”和“结束日期”,为此我使用了 onchange 函数。该函数正在工作并发出警告,但正在创建记录。实际上,如果有任何错误意味
给定平面中的一组点和一个不完整的 triangulation of the convex hull of the points (只给出了一些边),我正在寻找一种算法来完成三角剖分(初始给定的边应该保
我正在尝试获取constrains.maxHeight。 这是我使用的代码: LayoutBuilder( builder: (BuildContext context, B
我收到一个错误,我不明白为什么。 我的代码: library ieee; use ieee.std_logic_1164.all; use work.Func_Pack.all; use ieee.
我想启动很多任务来在 +-42Mio 记录的数据库上运行。我想以 5000 条记录/时间的批处理运行此程序(结果为 850 个任务)。我还想限制 java 开始为我执行此操作的线程数(最多 16 个)
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我收到一个错误,我不明白为什么。 我的代码: library ieee; use ieee.std_logic_1164.all; use work.Func_Pack.all; use ieee.
如果没有“可用数量”,我想禁止生产产品。但是这段代码不起作用。 它仅在我将 @api.constrains 更改为 @api.onchange('move_lines') 时有效,但如果我使用 onc
来自 C++ 背景(模板),我很难理解为什么以下 Swift 代码(泛型)无法编译: func backwards(array: [T]) -> [T] { let reversedColle
在 MSDN 上 - C# 编程指南 Constraints on Type Parameters ,它说: where T : interface_name The typeargument mus
我在 typescript 中有以下通用类 type UserId = number type Primitive = string | number | boolean class ColumnVa
尽管我通常是宏的粉丝,但是我不明白为什么Arduino的制造商选择使用宏而不是实际的函数来表示其一些算术“函数”。仅举几个例子: min() max() constrain() 他们的网站通知人们不要
您如何指定泛型类型参数只能是协议(protocol)(或符合该协议(protocol)的协议(protocol)),而不是符合该协议(protocol)的类? 例如: import Foundatio
这个问题由 C# 和 Salesforce 组成,双方可能都有解决方案。欢迎提出建议! 我正在编写一个通用类来读取 Salesforce 数据。签名看起来像这样: public abstract cl
我正在尝试通过 Interface Builder 使用约束来安排 Storyboard中的控件(“任何宽度,任何高度”的情况)。我在那里添加了 UIView,但是,当我根据给定的约束按下此元素的更新
我正在尝试实现某些目标,但我找不到/决定什么是最好的实现方法,所以我要问以前是否有人这样做过,或者 select2 是否内置了一些东西来实现目标我想要。 事情是这样的:我的 DOM 中有许多选择(多个
我在使用自适应支付的实时 API 凭据时遇到此错误。 阅读 intrwebs 和文档它必须对帐户权限做一些事情,但公平地说我不知道是哪一个。接收器、api 持有者或应用程序 我已经创建了应用程序,
我正在尝试编写一个可以从 Java 使用的 Clojure 库,而用户不知道它是用 Clojure 编写的。为此,我需要我的字段具有正确的类型: 我喜欢我能做到这一点: (deftype Point
我是一名优秀的程序员,十分优秀!