英文:
F# Add multiple constructor overloads that call the base class constructor
问题
I can help you with the translation. Here's the provided code snippet translated to Chinese:
我正在尝试在F#中创建具有结构相等性的字典,并且我希望通过几个字典构造函数重载。
在C#中,这将如下所示
```cs
class StructuralDictionary<Key, Value> : Dictionary<Key, Value> where Key : IEquatable<Key>
{
public StructuralDictionary() : base() { }
public StructuralDictionary(IDictionary<Key, Value> dict) : base(dict) { }
public StructuralDictionary(IEqualityComparer<Key> keyComparer) : base(keyComparer) { }
}
F#文档中有演示此用例的部分,但我无法使其编译
type StructuralDictionary<'key, 'value when 'key:equality> =
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
new () = { inherit Dictionary<'key, 'value> () }
Is there anything else I can assist you with?
<details>
<summary>英文:</summary>
I'm trying to create a dictionary with structural equality in F# and I want to pass through several dictionary constructor overloads.
In C# this would look like
```cs
class StructuralDictionary<Key, Value> : Dictionary<Key, Value> where Key : IEquatable<Key>
{
public StructuralDictionary() : base() { }
public StructuralDictionary(IDictionary<Key, Value> dict) : base(dict) { }
public StructuralDictionary(IEqualityComparer<Key> keyComparer) : base(keyComparer) { }
}
The F# docs have section demonstrating this usecase, but I can't get it to compile
type StructuralDictionary<'key, 'value when 'key:equality> =
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
new () = { inherit Dictionary<'key, 'value> () }
答案1
得分: 2
如果你想使用隐式构造函数语法,所有其他构造函数都必须调用隐式构造函数(并间接调用基类的相同构造函数)。
在你的示例中,你可以定义一个隐式构造函数,该构造函数以IDictionary
作为参数,并从其他构造函数中使用空字典调用它:
type StructuralDictionary<'TKey, 'TValue when 'TKey:equality>
(dictionary:IDictionary<'TKey, 'TValue>) =
inherit Dictionary<'TKey, 'TValue>(dictionary)
new () = StructuralDictionary(dict [])
与仅调用不带参数的基本构造函数相比,这可能会有一些小的开销(你正在分配一个空字典),但它允许你使用隐式构造函数的所有其他便捷特性,所以这可能是值得的。
英文:
If you want to use the implicit constructor syntax, all other constructors will have to call the implicit constructor (and, indirectly, will end up calling the same constructor of the base class).
In your example, you could define an implicit constructor that takes IDictionary
as the argument and call it with an empty dictionary from the other constructor:
type StructuralDictionary<'TKey, 'TValue when 'TKey:equality>
(dictionary:IDictionary<'TKey, 'TValue>) =
inherit Dictionary<'TKey, 'TValue>(dictionary)
new () = StructuralDictionary(dict [])
This may have some small overhead in contrast to just calling the base constructor without arguments (you are allocating an empty dictionary), but it lets you use all the other convenient features of implicit constructors, so it is probably worth it.
答案2
得分: 1
Ah. I was missing a inherit
at the top
type StructuralDictionary<'key, 'value when 'key:equality> =
inherit Dictionary<'key, 'value> // <--- This has to be here
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
new () = { inherit Dictionary<'key, 'value> () }
I got a bit confused because I started with a primary constructor like
type StructuralDictionary<'key, 'value when 'key:equality> () =
inherit Dictionary<'key, 'value> ()
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
But that doesn't work. So I tried to move the primary constructor down, but also moved the inheritance declaration so it still didn't compile.
It turns out to overload constructors you must have inherits at the top of the type declaration but you must not specify a primary constructor at the top.
英文:
Ah. I was missing a inherit
at the top
type StructuralDictionary<'key, 'value when 'key:equality> =
inherit Dictionary<'key, 'value> // <--- This has to be here
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
new () = { inherit Dictionary<'key, 'value> () }
I got a bit confused because I started with a primary constructor like
type StructuralDictionary<'key, 'value when 'key:equality> () =
inherit Dictionary<'key, 'value> ()
new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
But that doesn't work. So I tried to move the primary constructor down, but also moved the inheritance declaration so it still didn't compile.
It turns out to overload constructors you must have inherits at the top of the type declaration but you must not specify a primary constructor at the top.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论