Indexed Static Member Properties

Is it possible to create static indexed properties of an object in F #? MSDN only shows them for instance members, however I can define the following class:

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

But when I try to use it, I get an error message:

> ObjWithStaticProperty.StaticPropertyIndexed2.[1,2] <- 3;;

  ObjWithStaticProperty.StaticPropertyIndexed2.[1,2] <- 3;;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error FS1187: An indexer property must be given at least one argument

I tried several different syntax options and no one worked. It is also strange that when I sethover over in VS2010 for one of the type definitions, I get information about ExtraTopLevelOperators.set.

+3
source share
2 answers

I believe that you are invoking indexed properties using a different syntax (either instance or static):

ObjWithStaticProperty.StaticPropertyIndexed2(1,2) <- 3

- , Item x x.[...] (.. Item , ).

+4

Type.Prop.[args], Item:

type IndexedProperty<'I, 'T>(getter, setter) =
  member x.Item 
    with get (a:'I) : 'T = getter a
    and set (a:'I) (v:'T) : unit = setter a v

type ObjWithStaticProperty =
    static member StaticPropertyIndexed1 = 
      IndexedProperty((fun x -> 3), (fun x v -> ()))

ObjWithStaticProperty.StaticPropertyIndexed1.[0]

IndexedProperty, . , , , .

: , F # , , . ( , , , INotifyPropertyChange )

+5

All Articles