如何解决类型约束不匹配的问题,从C#到F# [英] how to solve type constraint mismatch, C# to F#

查看:97
本文介绍了如何解决类型约束不匹配的问题,从C#到F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些C#代码作为p#p应用程序的F#实现。.我必须承认,我对p2p实现没有完全的了解,我希望它与解决类型问题无关。 p2p库在C#中实现。

I'm trying to implement some C# code as F# for a p2p application.. I must admit I don't have full understanding of the p2p implementation and I hope it is irrelevant for fixing the type problem.. All of the p2p library is implemented in C#.

C#实现:

public class TestMessage : Message
{
    public string Text { get; set; }
}

...

var p = new Peer();
p.Subscribe(new Subscription<TestMessage>
{
    Callback = x => Console.WriteLine(x.Text),
});

基本思想是, p对等方现在订阅 TestMessage类型的消息,然后有一种类似的发布消息的方法。
'Subscribe'方法的签名为:

The basic idea is that the 'p' peer now subscribes to messages of the type 'TestMessage', and then there is a similar method for publishing messages.. The signature for the 'Subscribe' method is:

void Peer.Subscribe(ISubscription<Message> subscription)

ISubscription接口的定义:

The definition of the ISubscription interface:

public interface ISubscription<out T> where T : Message
{
    ICachingOptions CachingOptions { get; set; }
    IContact Contact { get; set; }
    string EntitySet { get; set; }
    string Key { get; }
    string Topic { get; }
    Type Type { get; }

    void InvokeCallback(Message message);
    ISerializableSubscription MakeSerializable();
    bool PredicateHolds(Message message);
}

F#实现:

type Xmsg(m:string) =
    inherit Message()
    member this.Text = m

let sub = Subscription<Xmsg>()
sub.Callback <- fun (m: Xmsg) -> System.Console.WriteLine(m.Text)
let p = new Peer()
p.Subscribe sub

最后一行导致以下错误:

The last line results in following errors:

The type 'Subscription<Xmsg>' is not compatible with the type 'ISubscription<Message>'

Type constraint mismatch. The type 
    Subscription<Xmsg>    
is not compatible with type
    ISubscription<Message>    
The type 'Subscription<Xmsg>' is not compatible with the type 'ISubscription<Message>'

我曾尝试用:>和:?>进行上下转换,但没有成功。.

I have tried messing around with :> and :?> for upcasting and downcasting, -but with no success..

我当然试图搜索解决方案,但是,要么没有解决方案,要么我不明白如何将其应用于我的问题...

Of course I have tried to search for a solution, however, either there are none or I don't understand how to apply them for my problem...

是否有解决方法?还是我应该放弃并为其创建一个C#库项目(并使用F#中的lib)? :)

Is there a fix?, or should I just give up and make a C# library project for it (and use that lib from F#)? :)

推荐答案

正如Ganesh所说,F#不支持协方差,这意味着您不能使用 ISubscription< DerivedType> ,其中在F#代码中需要 ISubscription< BaseType> 。但是,对协方差的支持已包含在运行时中,因此您可以通过一组强制转换来解决此问题:

As Ganesh says, F# doesn't support covariance, which means that you can't use an ISubscription<DerivedType> where an ISubscription<BaseType> is needed in F# code. However, support for covariance is baked into the runtime, so you can probably get around this with a set of casts:

p.Subscribe(box sub :?> ISubscription<_>)

在这里您正在投射 sub 一直到 obj ,然后向下转换回 ISubscription< Message> ,应该可以。

Here you're upcasting sub all the way to an obj and then downcasting back to an ISubscription<Message>, which ought to work.

这篇关于如何解决类型约束不匹配的问题,从C#到F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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