F# code organization: types & modules

后端 未结 4 1928
抹茶落季
抹茶落季 2020-12-24 02:52

How do you decide between writing a function inside a module or as a static member of some type?

For example, in the source code of F#, there are lots of types that

4条回答
  •  难免孤独
    2020-12-24 03:05

    In addition to the other answers there is one more case to use Modules:

    For value types they can help to define static properties that do not get re-evaluated every time they are accessed. for example:

    type [] Point =
        val x:float
        val y:float
        new (x,y) = {x=x;y=y}
    
        static member specialPoint1 = // sqrt is computed every time the property is accessed
            Point (sqrt 0.5 , sqrt 0.5 )
    
    []
    module Point = 
    
        let specialPoint2 = // sqrt is computed only once when the Module is opened
            Point (sqrt 0.5 , sqrt 0.5 )
    

提交回复
热议问题