Defining static classes in F#

后端 未结 5 721
故里飘歌
故里飘歌 2021-02-12 03:56

Is it possible to define a static class that contains overloadable members in F#? let module bindings cannot be overloaded, even though they are compiled into stati

5条回答
  •  醉酒成梦
    2021-02-12 04:05

    This is explained in The F# Component Design Guidelines.

    []
    type Demo =
        static member World = "World"
        static member Hello() = Demo.Hello(Demo.World)
        static member Hello(name: string) = sprintf "Hello %s!" name
    
    let s1 = Demo.Hello()
    let s2 = Demo.Hello("F#")
    

    It is still possible to define instance methods, but you can't instantiate the class when there is no constructor available.

    edit Jan 2021 : See the comment from Abel, and the linked issue. Joel Mueller's answer seems to be the best advice so far, but things will perhaps change in the future.

提交回复
热议问题