Search code examples
f#duck-typingstructural-typing

Why does this statically typed member access think it is a getter?


setter is a getter

rvm is an object, not a function.

Why does the constraint (^b:(member ReportSubTitle:(String -> unit)) rvm ) come up as a getter instead of a setter? Is this a Tooltip issue or something else?

Everywhere else where I've made use of statically typed generics, I find I don't even have to tell it the type at all. This doesn't work either:

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

Moved this attempt into a function and tried what @kvb suggested

no parens

This is the only use whatsoever of rvm in the enclosing scope/method


Solution

  • I'm not entirely sure what is the correct way for calling setters via static member constraints. I always find the feature a bit rough around the edges and prefer to use other options when possible (i.e. define an interface and access members via the interface).

    That said, it looks like you can use set_PropertyName in the constraint. The following small example works fine for me:

    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