F# 添加多个构造函数重载,调用基类构造函数

huangapple go评论52阅读模式
英文:

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&#39;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&lt;Key, Value&gt; : Dictionary&lt;Key, Value&gt; where Key : IEquatable&lt;Key&gt;
{
    public StructuralDictionary() : base() { }
    public StructuralDictionary(IDictionary&lt;Key, Value&gt; dict) : base(dict) { }
    public StructuralDictionary(IEqualityComparer&lt;Key&gt; keyComparer) : base(keyComparer) { }
}

The F# docs have section demonstrating this usecase, but I can't get it to compile

type StructuralDictionary&lt;&#39;key, &#39;value when &#39;key:equality&gt; =

    new (dictionary:IDictionary&lt;&#39;key,&#39;value&gt;) = { inherit Dictionary&lt;&#39;key,&#39;value&gt;(dictionary) }
    new () = { inherit Dictionary&lt;&#39;key, &#39;value&gt; () }

答案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&lt;&#39;TKey, &#39;TValue when &#39;TKey:equality&gt;
    (dictionary:IDictionary&lt;&#39;TKey, &#39;TValue&gt;) =
  inherit Dictionary&lt;&#39;TKey, &#39;TValue&gt;(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&lt;&#39;key, &#39;value when &#39;key:equality&gt; =
    inherit Dictionary&lt;&#39;key, &#39;value&gt; // &lt;--- This has to be here

    new (dictionary:IDictionary&lt;&#39;key,&#39;value&gt;) = { inherit Dictionary&lt;&#39;key,&#39;value&gt;(dictionary) }
    new () = { inherit Dictionary&lt;&#39;key, &#39;value&gt; () }

I got a bit confused because I started with a primary constructor like

type StructuralDictionary&lt;&#39;key, &#39;value when &#39;key:equality&gt; () =
    inherit Dictionary&lt;&#39;key, &#39;value&gt; () 

    new (dictionary:IDictionary&lt;&#39;key,&#39;value&gt;) = { inherit Dictionary&lt;&#39;key,&#39;value&gt;(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&lt;&#39;key, &#39;value when &#39;key:equality&gt; =
    inherit Dictionary&lt;&#39;key, &#39;value&gt; // &lt;--- This has to be here

    new (dictionary:IDictionary&lt;&#39;key,&#39;value&gt;) = { inherit Dictionary&lt;&#39;key,&#39;value&gt;(dictionary) }
    new () = { inherit Dictionary&lt;&#39;key, &#39;value&gt; () }

I got a bit confused because I started with a primary constructor like

type StructuralDictionary&lt;&#39;key, &#39;value when &#39;key:equality&gt; () =
    inherit Dictionary&lt;&#39;key, &#39;value&gt; ()

    new (dictionary:IDictionary&lt;&#39;key,&#39;value&gt;) = { inherit Dictionary&lt;&#39;key,&#39;value&gt;(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.

huangapple
  • 本文由 发表于 2023年4月1日 00:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定