【发布时间】:2011-03-23 04:28:04
【问题描述】:
是否可以在 F# 中创建静态成员索引属性? MSDN 仅针对实例成员显示它们,但是,我可以定义以下类:
type ObjWithStaticProperty =
static member StaticProperty
with get () = 3
and set (value:int) = ()
static member StaticPropertyIndexed1
with get (x:int) = 3
and set (x:int) (value:int) = ()
static member StaticPropertyIndexed2
with get (x:int,y:int) = 3
and set (x:int,y:int) (value:int) = ()
//Type signature given by FSI:
type ObjWithStaticProperty =
class
static member StaticProperty : int
static member StaticPropertyIndexed1 : x:int -> int with get
static member StaticPropertyIndexed2 : x:int * y:int -> int with get
static member StaticProperty : int with set
static member StaticPropertyIndexed1 : x:int -> int with set
static member StaticPropertyIndexed2 : x:int * y:int -> int with set
end
但是当我尝试使用一个时,我得到一个错误:
> ObjWithStaticProperty.StaticPropertyIndexed2.[1,2] <- 3;;
ObjWithStaticProperty.StaticPropertyIndexed2.[1,2] <- 3;;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error FS1187: An indexer property must be given at least one argument
我尝试了几种不同的语法变体,但都没有奏效。同样奇怪的是,当我在 VS2010 中将鼠标悬停在 set 上以获取类型中的定义之一时,我会得到有关 ExtraTopLevelOperators.set 的信息。
【问题讨论】:
标签: f#