y 型で非常に奇妙なことが起こっており、これが正当化されるかどうかはまったくわかりません。私はそうではないと思う傾向があります。
このコードは正常に動作します:
type DictionarySingleton private () =
static let mutable instance = Dictionary<string*obj, obj>()
static member Instance = instance
let memoize (f:'a -> 'b) =
fun (x:'a) ->
let key = f.ToString(), (x :> obj)
if (DictionarySingleton.Instance).ContainsKey(key)
then let r = (DictionarySingleton.Instance).[key]
r :?> 'b
else
let res = f x
(DictionarySingleton.Instance).[key] <- (res :> obj)
res
そしてこいつらは文句を言う
type DictionarySingleton private () =
static let mutable instance = Dictionary<string*obj, _>()
static member Instance = instance
let memoize (f:'a -> 'b) =
fun (x:'a) ->
let key = f.ToString(), (x :> obj)
if (DictionarySingleton.Instance).ContainsKey(key)
then let r = (DictionarySingleton.Instance).[key]
r :?> 'b
else
let res = f x
(DictionarySingleton.Instance).[key] <- (res :> obj)
res
違いは、辞書定義のアンダースコアのみです。推論される型は同じですが、r から型 'b への動的キャストでエラーが発生します。
「この実行時強制...一部の型では実行時型テストは許可されていません..」
私は何かが欠けていますか、それとも大まかなエッジですか?