【问题标题】:Why does this statically typed member access think it is a getter?为什么这个静态类型的成员访问认为它是一个 getter?
【发布时间】:2023-03-23 03:53:01
【问题描述】:

rvm 是一个对象,而不是一个函数。

为什么约束(^b:(member ReportSubTitle:(String -> unit)) rvm ) 是作为getter 而不是setter?这是工具提示问题还是其他问题?

在我使用静态类型泛型的所有其他地方,我发现我什至根本不需要告诉它类型。这也不起作用:

// Setup the sub title with the total
let subTitleSetter = (^b:(member ReportSubTitle:_) rvm ) 
subTitleSetter ("Total: " + total.ToString("C") )

将此尝试移动到一个函数中并尝试了@kvb 的建议

这是rvm 在封闭范围/方法中的唯一用途

【问题讨论】:

  • 尝试不使用成员类型周围的括号:ReportSubTitle:String->unit。这将方法与函数值属性区分开来。
  • 试过了,“预期 2 个表达式,得到 1 个”
  • 您必须同时提供rvmtest 作为参数(作为语法元组)。

标签: f# duck-typing structural-typing


【解决方案1】:

我不完全确定通过静态成员约束调用设置器的正确方法是什么。我总是发现该功能的边缘有点粗糙,并且在可能的情况下更喜欢使用其他选项(即定义接口并通过接口访问成员)。

也就是说,您似乎可以在约束中使用set_PropertyName。下面的小例子对我来说很好:

type A() = 
  member val Foo = 0 with get, set

let a = A()
let setter v = (^b:(member set_Foo : int -> unit) (a, v) )

setter 42
a.Foo

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多