【发布时间】:2016-01-25 04:46:49
【问题描述】:
我正在尝试定义一个包含带有运算符重载的泛型类型的模块,但单声道编译器似乎忽略了我在实现文件中的类型。我的接口文件如下所示:
module Vector
[<Sealed>]
type Vector<'a> =
static member ( +. ) : Vector<'a> * Vector<'a> -> Vector<'a>
val make : 'a * 'a -> Vector<'a>
val coord : Vector<'a> -> 'a * 'a
而我的实现是
module Vector
type Vector<'a> =
| V of 'a * 'a
static member ( +. ) (V(x1,y1), V(x2,y2)) = V(x1+x2, y1+y2)
let make (x, y) = V(x, y)
let coord (V(x, y)) = (x, y)
当我编译时,我得到:
fsharpc -a VectorParam.fsi VectorParam.fs
F# Compiler for F# 3.1 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License
/.../VectorParam.fs(4,19): error FS0034: Module 'Vector' contains
static member Vector.( +. ) : Vector<int> * Vector<int> -> Vector<int>
but its signature specifies
static member Vector.( +. ) : Vector<'a> * Vector<'a> -> Vector<'a>
The types differ
我不明白,因为我使用的是标记类型。而且,如果我删除所有“”并将剩余的类型别名替换为浮点数,那么一切正常。谁能帮我理解正确的语法?
谢谢,乔恩
【问题讨论】:
-
仅供参考,fsharpc (fsc.exe) 由 Xamarin 和 Mono 分发,但它来自 fsharp.org / github.com/fsharp/fsharp 这是来自 MS 的 xplat 版本 (github.com/Microsoft/visualfsharp)
标签: generics f# mono operator-overloading inline