F# 向F中的构造函数添加字典键

F# 向F中的构造函数添加字典键,f#,dictionary,F#,Dictionary,因为我对F是新来的,这似乎是一个基本的问题。但事情是这样的。我有一个使用以下代码的构造函数类: new () = { _index = 0; _inputString = ""; _tokens = new Dictionary<string, string>() { {"key", "value"} } } 除了F似乎不允许我在字典中添加标记外,其他一切都正常。我可以用一个新的Dictionary对象初始化它,但是如果我尝试填充,它会抛

因为我对F是新来的,这似乎是一个基本的问题。但事情是这样的。我有一个使用以下代码的构造函数类:

new () = { 
    _index = 0; _inputString = ""; 
    _tokens = new Dictionary<string, string>() {
        {"key", "value"}
    }
}
除了F似乎不允许我在字典中添加标记外,其他一切都正常。我可以用一个新的Dictionary对象初始化它,但是如果我尝试填充,它会抛出一个错误。我也无法使用.Add成员执行此操作。我见过F构造函数初始化字段值的例子,但是可能没有办法执行其他代码吗?

因为字典有,所以您可以使用内置函数来帮助您:

open System.Collections.Generic

type Foo =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () =
        {
            _index = 0
            _inputString = ""
            _tokens = Dictionary(dict [("fooKey", "fooValue")])
        }
但是,也可以在构造函数的对象初始值设定项之前或之后执行非平凡代码:

type Bar =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () =
        let tokens = Dictionary()
        tokens.Add ("barKey", "barValue")
        {
            _index = 0
            _inputString = ""
            _tokens = tokens
        }

type Baz =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () as this =
        {
            _index = 0
            _inputString = ""
            _tokens = Dictionary()
        } then
        this._tokens.Add ("bazKey", "bazValue")
由于Dictionary具有以下功能,因此您可以使用内置函数来帮助您:

open System.Collections.Generic

type Foo =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () =
        {
            _index = 0
            _inputString = ""
            _tokens = Dictionary(dict [("fooKey", "fooValue")])
        }
但是,也可以在构造函数的对象初始值设定项之前或之后执行非平凡代码:

type Bar =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () =
        let tokens = Dictionary()
        tokens.Add ("barKey", "barValue")
        {
            _index = 0
            _inputString = ""
            _tokens = tokens
        }

type Baz =
    val _index       : int
    val _inputString : string
    val _tokens      : Dictionary<string, string>
    new () as this =
        {
            _index = 0
            _inputString = ""
            _tokens = Dictionary()
        } then
        this._tokens.Add ("bazKey", "bazValue")

Ildjarn已经回答了您的问题,但让我补充一点关于编码风格的注意事项——我认为现在大多数F程序更喜欢隐式构造函数语法,在这种语法中,您定义一个隐式构造函数作为类型声明的一部分。这通常会使代码简单得多。你可以这样写:

type Bah() = 
  let index = 0
  let inputString = ""
  let tokens = new Dictionary<string, string>()
  do tokens.Add("bazKey", "barValue")

  member x.Foo = "!"
type Baf(index:int, inputString:string, tokens:Dictionary<string, string>) =
  new() = 
    let tokens = new Dictionary<string, string>()
    tokens.Add("bazKey", "barValue")
    Baf(0, "", tokens)
这里有两个构造函数——一个少参数,一个接受三个参数。还可以将隐式构造函数设为私有,并仅公开更具体的情况:

type Baf private (index:int, inputString:string, tokens:Dictionary<string, string>) =
  // (...)

作为补充说明,我还将命名从_index改为index,因为我不认为F指南建议使用下划线,尽管对于使用val声明的字段可能有意义,Ildjarn已经回答了您的问题,但是,让我补充一点关于编码风格的注意事项——我认为现在大多数F程序更喜欢隐式构造函数语法,在这种语法中,您将定义一个隐式构造函数作为类型声明的一部分。这通常会使代码简单得多。你可以这样写:

type Bah() = 
  let index = 0
  let inputString = ""
  let tokens = new Dictionary<string, string>()
  do tokens.Add("bazKey", "barValue")

  member x.Foo = "!"
type Baf(index:int, inputString:string, tokens:Dictionary<string, string>) =
  new() = 
    let tokens = new Dictionary<string, string>()
    tokens.Add("bazKey", "barValue")
    Baf(0, "", tokens)
这里有两个构造函数——一个少参数,一个接受三个参数。还可以将隐式构造函数设为私有,并仅公开更具体的情况:

type Baf private (index:int, inputString:string, tokens:Dictionary<string, string>) =
  // (...)

作为旁注,我还将命名从_index改为index,因为我不认为F指南建议使用下划线,尽管在F中使用val声明的字段可能有意义,但所有内容都是一个表达式,因此可以像这样初始化_标记:


在F中,一切都是一个表达式,因此可以像这样初始化_标记:


您为可变对象赋值的语法错误。如果您分配的是可变的,您应该只使用一个注释,您可以在F中创建一个不可变的字典,如下所示:dict[key,value]。@vcsjones-分配语法在这里是正确的:这是一个对象初始值设定项,不清楚要初始化的字段是否可变,但这两种情况下都是有效的。您分配给可变对象的语法是错误的。如果您正在分配可变的内容,您应该只使用注释,您可以在F中创建一个不可变的字典,如下所示:dict[key,value]。@vcsjones-分配语法在这里是正确的:这是一个对象初始值设定项,不清楚要初始化的字段是否可变,但在任何情况下都是有效的。哦,这对我来说是一个新功能,太棒了。@ildjarn-事实上,在回到基于语句的语言时,能够以这种方式初始化变量是我最怀念的功能之一。我不会想到这一点,但这很好。哦,这对我来说是一个新功能,太棒了。@ildjarn-的确,当我回到基于语句的语言时,能够以这种方式初始化变量是我最怀念的特性之一。