F# class 将接口实现为私有成员,为什么?

F# class implements interface as private member, why?

我决定今天用 F# 练习,发现一件有趣的事。我将在 C# 和 F# 中提供类似的代码,但使用 DAO(数据访问对象)时行为会有所不同:

C# 版本:

interface IPoint2D
{
    int X { get; set; }
    int Y { get; set; }
}

class Point2D : IPoint2D
{
    private int _x = 0;
    private int _y = 0;

    public int X { get { return _x; } set { _x = value; } }
    public int Y { get { return _y; } set { _y = value; } }
}

F# 版本:

type IPoint2D =
    abstract member X: int
    abstract member Y: int

type Point2D(x: int, y: int) = 
    let _x = 0
    let _y = 0

    interface IPoint2D with
        member this.X = x
        member this.Y = y

第一个区别非常明显,即使用 C#,我必须将成员声明为 public 以实现合同。

但是,为什么 F# 允许将接口实现为私有成员?有什么意义?

F# 要求向上转换对象以便直接访问它们的接口。 let x = (firstPoint :> IPoint2D).X

F# 支持函数参数的隐式接口转换。它过去需要泛型,但已在较新版本的语言中更新。

let Distance (a:IPoint2D) (b:IPoint2D) =
    (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y)
    |> float |> Math.Sqrt

let firstPoint = new Point2D(2, 3)
let secondPoint = new Point2D(4, 5)

let distance = Distance firstPoint secondPoint
printf "%f" distance

并非所有人都同意,但作为设计规则,不应向上转换对象以直接访问其接口。这样做会增加代码耦合。如果编写的代码调用对象接口上的方法,则在不更新所有调用对象的情况下,无法轻松更改对象 class。

如果界面是带有 x、y、z 的 IPoint3D 并且您想将其更改为 IPoint2D,那么必须更新所有引用 z 的强制转换,而不仅仅是像 Distance 这样的界面使用者。

我相信这种设计选择是为了与语言的其余部分保持一致,并在使用 OOP 时促进松耦合。 2014 年有一个 user voice feature request 请求隐式向上转换但没有得到答复。