2017-07-28 79 views
1

考慮:F# - 警告說,我不聲明類型參數明確

type Service = Object 
type Services = ConcurrentDictionary<Type, Service> 

爲什麼會這樣:

// If we use this form, we get a compiler warning: 
type GetService<'T> = Services -> 'T 
let GetService:GetService<'T> = fun services -> 
    services.[typeof<'T>] :?> 'T 

產生警告:

「的方法或函數 '的GetService'不應該給出明確的類型參數,因爲它沒有聲明它的類型參數明確性。「

但這並不:

let GetService<'T> (services : Services) = 
    services.[typeof<'T>] :?> 'T 

用法示例:

let someService = services |> GetService<SomeServiceType> 

如果我要糾正的第一個版本,以免得到警告,我怎麼會改變的定義?

回答

2

只要給它一個類型參數,就像錯誤消息所示。這應該工作:

let GetService<'T> : GetService<'T> = fun services -> 
    services.[typeof<'T>] :?> 'T 
+0

是的,工作。謝謝! –