2016-02-01 25 views
1

說我有它定義兩個函數,Function1Function2和我使用對象表達式來實現它的接口ICache,但我也希望添加一個輔助函數:F#添加非接口功能到對象表達式

let WebCache = 
    { new ICache with 
     member __.HelperFunction = //this doesn't work! 
     member __.Function1 = foo 
     member __.Function2 = bar 
    } 

F#似乎不允許你添加任何不屬於界面的方法。有沒有解決方法?如果我想這樣做,我是不是應該首先使用對象表達式?

回答

3

您可以定義輔助函數作爲一個普通的(本地)函數的對象表達之外:

let WebCache = 
    let helper n = 
     printfn "Helping %" n 
    { new ICache with 
     member __.Function1 = helper 1 
     member __.Function2 = helper 2 } 
+0

啊哈,就這麼簡單!謝謝。 – jackmott