在不同的通用实例化中实现相同的接口 [英] Implementing the same interface at different generic instantiations

查看:18
本文介绍了在不同的通用实例化中实现相同的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我可以在一个类上实现两次泛型接口,使用两个不同的类型参数:

In C#, I can implement a generic interface twice on one class, using two different type-parameters:

interface IFoo<T> { void Foo(T x); }

class Bar : IFoo<int>, IFoo<float>
{
    public void Foo(int x) { }
    public void Foo(float y) { }
}

我想在 F# 中做同样的事情:

I would like to do the same thing in F#:

type IFoo<'a> = abstract member Foo : 'a -> unit

type Bar() =
    interface IFoo<int> with 
        [<OverloadID("int")>]
        member this.Foo x = ()

    interface IFoo<float> with 
        [<OverloadID("float")>]
        member this.Foo x = ()

但它给出了一个编译器错误:

But it gives a compiler error:

此类型在不同的通用实例'IFoo''IFoo' 处实现或继承了相同的接口.这在此版本的 F# 中是不允许的.

This type implements or inherits the same interface at different generic instantiations 'IFoo<float>' and 'IFoo<int>'. This is not permitted in this version of F#.

我找不到任何 对此问题的讨论 在网络上.出于某种原因,这种使用是否令人不悦?是否计划在即将发布的 F# 版本中允许这样做?

I can't find any discussion of this issue on the web. Is such use frowned upon for some reason? Are there plans to allow this in an upcoming release of F#?

推荐答案

目前我不知道允许这样做的计划..功能 已计划 并且至少部分(见评论)在 F# 4.0 中实现.

Right now I don't know of plans to allow this.. The feature has been planned and is, at least partially (see comments) implemented in F# 4.0.

我认为它目前被禁止的唯一原因是它的实现并不简单(尤其是使用 F# 类型推断),而且它在实践中很少出现(我只记得一个客户曾经问过这个问题).

I think the only reasons its currently disallowed are that it's non-trivial to implement (especially with F# type inference), and it rarely arises in practice (I only recall one customer ever asking about this).

考虑到无限的时间和资源,我认为这是允许的(我可以想象这会被添加到该语言的未来版本中),但现在看来这不是一个值得努力的功能支持.(如果您知道一个强有力的激励案例,请发送邮件至 fsbugs@microsoft.com.)

Given an infinite amount of time and resources, I think this would be allowed (I can imagine this being added to a future version of the language), but right now it does not seem like this is a feature worth the effort of supporting. (If you know a strong motivating case, please mail fsbugs@microsoft.com.)

编辑

为了好奇,我写了这个 C#:

As an experiment for the curious, I wrote this C#:

public interface IG<T>
{
    void F(T x);
}
public class CIG : IG<int>, IG<string>
{
    public void F(int x) { Console.WriteLine("int"); }
    public void F(string x) { Console.WriteLine("str"); }
}

并从 F# 引用它(带有建议结果的注释)

and referenced it from F# (with comments suggesting the results)

let cig = new CIG()
let idunno = cig :> IG<_>  // type IG<int>, guess just picks 'first' interface?
let ii = cig :> IG<int>    // works
ii.F(42)                   // prints "int"
let is = cig :> IG<string> // works
is.F("foo")                // prints "str"

所以这就是在 F# 的边界"内容上通常会发生的情况 - F# 可以正常使用这些内容,即使您无法从语言内部创作相同的内容.

so this is what typically happens on this 'boundary' stuff with F# - F# can consume this stuff ok, even if you can't author the same stuff from within the language.

这篇关于在不同的通用实例化中实现相同的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆