Google は、F# ディクショナリ (またはその他のコレクション) にエントリを追加および削除する例を数多く提供しています。しかし、私は同等の例を見ません
myDict["Key"] = MyValue;
私はもう試した
myDict.["Key"] <- MyValue
また、辞書を次のように宣言しようとしました
Dictionary<string, mutable string>
これにはいくつかのバリエーションがあります。しかし、私はまだ正しい組み合わせを見つけていません...実際にF#で可能であれば.
編集:問題のあるコードは次のとおりです。
type Config(?fileName : string) =
let fileName = defaultArg fileName @"C:\path\myConfigs.ini"
static let settings =
dict[ "Setting1", "1";
"Setting2", "2";
"Debug", "0";
"State", "Disarray";]
let settingRegex = new Regex(@"\s*(?<key>([^;#=]*[^;#= ]))\s*=\s*(?<value>([^;#]*[^;# ]))")
do File.ReadAllLines(fileName)
|> Seq.map(fun line -> settingRegex.Match(line))
|> Seq.filter(fun mtch -> mtch.Success)
|> Seq.iter(fun mtch -> settings.[mtch.Groups.Item("key").Value] <- mtch.Groups.Item("value").Value)
私が得ているエラーは次のとおりです。
System.NotSupportedException: This value may not be mutated
at Microsoft.FSharp.Core.ExtraTopLevelOperators.dict@37-2.set_Item(K key, V value)
at <StartupCode$FSI_0036>.$FSI_0036_Config.$ctor@25-6.Invoke(Match mtch)
at Microsoft.FSharp.Collections.SeqModule.iter[T](FastFunc`2 action, IEnumerable`1 sequence)
at FSI_0036.Utilities.Config..ctor(Option`1 fileName)
at <StartupCode$FSI_0041>.$FSI_0041.main@()
stopped due to error