【发布时间】:2014-10-13 17:58:13
【问题描述】:
我有一个基类
type MyBase() =
let param = myfun()
member this.Param = param
type MyInherited() =
inherit MyBase()
do
base.Param.DoSomething() // cannot access base in let or do binding.
我想在继承的对象实例化过程中准确地调用一次DoSomething。但在这里我是不允许的。那我该怎么办?
我是否必须创建一个方法
member this.DoSomething() = base.Param.DoSomething()
并在构造函数中调用它?
type MyInherited() as self =
inherit MyBase()
do
self.DoSomething()
感觉有点怪,重复了
更新
我最初的简化示例不合适。检查以下内容:
type MyBase() =
let param = "test"
member this.Param = param
type MyInherited() =
inherit MyBase()
do
(fun () -> base.Param) () |> ignore // Doesn't work,
// A protected member is called or 'base' is being used.
// This is only allowed in the direct implementation of members
// since they could escape their object scope.
type MyInherited() as self =
inherit MyBase()
do
(fun () -> self.Param) () |> ignore // Works
现在实际上好多了,我需要做的就是使用self而不是base...(我不必重新定义Param,因为它已经被继承了。)
这里解释F#有这样限制的原因:
why is base only possible in private members?
但我仍然不清楚为什么 base 不能在闭包中使用,尽管它可以在简单的 let 绑定中访问。
【问题讨论】:
-
Param 属性的类型是什么? DoSomething() 的返回值是多少?我无法重现你的情况。它似乎适用于简单的属性
标签: f#