1
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>
如果我要糾正的第一個版本,以免得到警告,我怎麼會改變的定義?
是的,工作。謝謝! –