1 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果地圖中的函數(shù)(即HOLD()、INB()、 等)不需要比 StatModel 接口提供的更多的模型訪問權(quán)限,那么您的需求應(yīng)該可以通過更改這些函數(shù)來(lái)接收 StatModel 而不是 NoaggRow 來(lái)實(shí)現(xiàn):
func HOLD(s StatModel) float64 {
...
}
那么你要添加的AcsiRow函數(shù)可以有相同的簽名,statCount和Count可以改寫如下:
func statCount(model StatModel, f func(StatModel) float64) float64 {
countedStat := f(model)
return countedStat
}
func Count(model StatModel, name string) float64 {
m := map[string]func(StatModel) float64 {
"HOLD" : HOLD,
"INB" : INB,
"AHT" : AHT,
"RING" : RING,
"TALK" : TALK,
"ACW" : ACW,
"OCC" : OCC,
// Map AcsiModel functions here, let's call them ACSIn here
// (you mentioned that the names would be different, so
// I assume they don't get in the way of the functions
// above):
"ACSI1": ACSI1,
"ACSI2": ACSI2,
"ACSI3": ACSI3,
}
countedStat := statCount(model, m[name])
return countedStat
}
免責(zé)聲明:這只是基于我在帖子中看到的內(nèi)容的一個(gè)想法。我對(duì) Revel 一無(wú)所知,因此也許我可能錯(cuò)過了一些由 Revel 上下文引起的重要細(xì)節(jié),這些細(xì)節(jié)會(huì)阻止實(shí)施這種方法。
根據(jù)評(píng)論更新:
使用類型的斷言,以使HOLD,OCC等功能,以訪問結(jié)構(gòu)特定的屬性,像這樣:
func HOLD (s StatModel) float64 {
noagg, ok := s.(NoaggRow) // typecast from StateModel to NoaggRow
if !ok {
// Looks like the caller did not pass a matching model/name pair into Count().
// Error handling...
}
attr := noagg.AttributeSpecificToNoaggRow
...
}
這在運(yùn)行時(shí)可能看起來(lái)有點(diǎn)危險(xiǎn),但如果調(diào)用者總是將匹配的模型/名稱值對(duì)傳遞給 Count()(我假設(shè)它這樣做了),則類型轉(zhuǎn)換應(yīng)該是安全的。
- 1 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報(bào)