【问题标题】:F#: Implementing interface with function name the same as the keyword beginF#:实现函数名与关键字begin相同的接口
【发布时间】:2011-04-21 07:29:41
【问题描述】:

我正在尝试实现作为 Microsoft.VisualC.StlClr 命名空间一部分的 IVector 接口。它有一个成员函数 begin()。如果我尝试实现该接口,那么它会抱怨“对象表达式中出现意外的关键字'开始'”。

这是因为 begin 是一个关键字,而我无法实现具有该名称的成员函数吗?

乙。

【问题讨论】:

    标签: f# keyword identifier


    【解决方案1】:

    您可以尝试将其包装成反引号。 IVector 接口太大,无法将其作为示例实现,因此示例会更小 - 下面的代码编译时没有任何错误。

    // C#
    public interface ITest
    {
        void begin();
    }
    
    // F#
    type Test() = 
        interface UStatic.ITest with
            member this.``begin``() = ()
    

    【讨论】:

    • 那么您每次都必须将其称为Test.''begin'' 吗? (使用引号无法解决如何逃避反引号)
    • @user:是的,每次使用时都必须将名称用反引号括起来。向调用它并具有更合理名称的 Test 类型添加其他成员可能是一个好主意。
    • 还有一个问题。这是我的实现: member this.begin(value:byref>) = () 接口定义是这样的: abstract end : :ContainerRandomAccessIterator byref -> unit 我正在编译错误提示:“成员开始:值:byref> -> 单元没有正确的类型来覆盖相应的抽象方法。”
    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2012-11-28
    • 2011-03-05
    • 1970-01-01
    • 2022-01-27
    • 2011-12-13
    相关资源
    最近更新 更多