特别是,我想为这种类型写一个扩展方法:
type Frame<'TRowKey, string when 'TRowKey : equality> with
member frame.someMethod =
// code
使用该代码,我收到此错误:
Unexpected identifier in type name. Expected infix operator, quote symbol or other token.
替换 string
与 String
给出相同的结果。
原始类型是Frame<'TRowKey, 'TColumnKey (requires equality and equality)>
来自 Deedle 图书馆。
最佳答案
@Gustavo 对如何使用扩展方法给出了详尽的回答。但是,如果您没有特别充分的理由认为这是一种扩展方法(如 C# 互操作),您可能只使用一个简单的函数:
type Frame<'row, 'col> = { row: 'row; col: 'col }
module Frame =
let restricted (frame: Frame<'row, string>) = frame
Frame.restricted { row = 3; col = "test" } // compiles
Frame.restricted { row = 3; col = 5 } // doesn't
在编写纯 F# 代码时,我认为这是一种更简洁的方法 - 当绑定(bind)函数作为类型的固有部分没有意义时,它们比方法更可取,而且来自属性的干扰更少。
关于generics - 如何为泛型类型编写扩展方法,其中一个类型变量必须是字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867440/