【问题标题】:Inheriting class not seeing base class继承类看不到基类
【发布时间】:2021-10-04 19:32:51
【问题描述】:

我在一个文件中有一个抽象类:

namespace Dac.V5

[<AbstractClass>]
type DacRecord(recordType: int16) =
    do
        if recordType < 0s then
            invalidArg (nameof recordType) "Argument out of range"

    member this.Type = recordType

以及相邻文件中的继承类:

namespace Dac.V5

[<AbstractClass>]
type DacReturnRecord(recordType: int16, decodedData: byte seq) =
    inherit DacRecord(recordType)

    let data: byte seq = decodedData

    member this.Data with get() = data and set(v) = data <- v

但是,在继承类中,基类似乎未定义,inherit 语句出现错误。

这些文件位于同一个子目录中,并以其所属的类命名。

【问题讨论】:

  • 您的 .fsproj 文件中列出的文件的顺序是什么?包含派生类的文件必须出现在包含基类的文件之后。我怀疑你的顺序相反。
  • 这是一个奇怪的要求;我不相信 C# 会那样做。真的有必要吗?
  • F# 要求代码按依赖顺序排列。也就是说,您不能对编译器尚未看到的代码使用前向引用。对于从 C# 迁移到 F# 的人们来说,这是一个常见的障碍。起初我也对此感到恼火,但后来认为这是一个合理的决定。你可以找到一个很好的解释here

标签: class inheritance f# undefined


【解决方案1】:

如 cmets 中所述,您需要对文件进行排序,以便基类位于继承的之前。一旦你这样做了,它应该可以工作,除了你需要将data标记为mutable

[<AbstractClass>]
type DacReturnRecord(recordType: int16, decodedData: byte seq) =
    inherit DacRecord(recordType)

    let mutable data: byte seq = decodedData

    member this.Data with get() = data and set(v) = data <- v

【讨论】:

  • 是的,这就是解决方案。无论如何,这仍然令人费解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
相关资源
最近更新 更多