0
我試圖在F#程序集中構建log4net樣式接口。關鍵屬性是公開一個返回對象實例的靜態方法。 log4net利用C#委託來完成任務,例如與LogManager.GetLogger("log123")
。根據我的理解,代表們不如功能 - 作爲面向內部的F#庫的頭等艙。F#中的log4net樣式接口 - 靜態方法返回對象的實例
下面的簡化代碼完成了目標,但我不習慣使用F#引用單元來保存實例化對象的映射。我有興趣反饋我的不適是否有必要。
namespace Test
[<Interface>]
type IMyIface =
abstract member Addi : int -> int
[<Sealed>]
type TheMainObject internal (x:int) =
let mutable sum = x
interface IMyIface with
member this.Addi(y:int) = sum <- sum + y; sum
module internal Wrapr =
let mymap = ref Map.empty
let mgr s =
let m = !mymap
if Map.containsKey s m then m.[s]
else
let x = new TheMainObject(0)
mymap := m.Add(s, x)
x
[<Sealed>]
type Mgr() =
static member Get(n:string) =
Wrapr.mgr n :> IMyIface
Program.fs調用庫上面如下:提前徵求意見
open Test
let a = Mgr.Get("hello")
printfn "%d" (a.Addi(1))
let c = Mgr.Get("hello")
printfn "%d, %A" (c.Addi(3)) (a = c) //prints 4, true
感謝。