2015-11-12 38 views
2

爲什麼我不能將None添加到System.Collections.Generic.DictionaryOption?這是Mono中的預期行爲還是錯誤?F#字典選項

F# Interactive for F# 3.1 (Open Source Edition) 
Freely distributed under the Apache 2.0 Open Source License 

For help type #help;; 

> open System.Collections.Generic;; 
> let d1 = new Dictionary<int option, int>();; 

val d1 : Dictionary<int option,int> = dict [] 

> d1.Add(None, 1);; 
System.ArgumentNullException: Value cannot be null. 
Parameter name: key 
    at System.ThrowHelper.ThrowArgumentNullException (ExceptionArgument argument) in <filename unknown>:line 0 
    at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value, Boolean add) in <filename unknown>:line 0 
    at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value) in <filename unknown>:line 0 
    at <StartupCode$FSI_0004>[email protected]() in <filename unknown>:line 0 
    at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) 
    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) in <filename unknown>:line 0 
Stopped due to error 
> d1.Add(Some 10, 1);; 
val it : unit =() 

我使用的是單聲道在OS X

$ mono --version 
Mono JIT compiler version 4.2.0 (Stable 4.2.0.179/a224653 Tue Oct 6 11:28:25 PDT 2015) 
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com 
    TLS:   normal 
    SIGSEGV:  altstack 
    Notification: kqueue 
    Architecture: amd64 
    Disabled:  none 
    Misc:   softdebug 
    LLVM:   supported, not enabled. 
    GC:   sgen 
+0

嘗試'讓d1 = new Dictionary (HashIdentity.Structural)'。 – ildjarn

+0

它不起作用。 –

+1

@ Ming-Tang不幸的是,正如凱文所說,這是理想的行爲。定期'字典<_,_>'不接受'空'作爲關鍵。另一方面,F#的地圖<_,_>'按預期工作,但我知道它可能不是一個合適的替代品。 –

回答