我正在学习 F#,但我不知道如何实现这一目标。
我有一个界面:
type IRule =
abstract member Getrule : keyfield:string -> bool * 'T
在 C# 中:
public interface IRule
{
Tuple<bool, T> GetRule<T>(string keyFieldIdentifier);
}
我需要创建一个实现此接口(interface)的类
C#:
public class CustomRule : IRule
{
Tuple<bool, T> IRule.GetRule<T>(string keyFieldIdentifier)
{
var _result = SomeHelper.CreateResult(keyFieldIdentifier);
return new Tuple<bool, T>(_result.Item1, (T)(object)_result.Item2);
}
}
F#:
type SomeClass() =
interface IRule with
member this.Getrule<'T>(keyfield) =
let value = 3
(true, value :> 'T)
我不知道最后一行(true, value :> 'a)
怎么写,如何转换为泛型类型?
错误提示:
错误 1 来自类型的静态强制转换
整数
到
'T
涉及基于该程序点之前的信息的不确定类型。某些类型不允许静态强制转换。
最佳答案
这将编译 - 但我不太确定它是否会做你想要的事情
type SomeClass() =
interface IRule with
member this.Getrule<'T>(keyfield):bool * 'T =
let value = 3
(true,unbox box value )
特别是,这不会进行强制转换,并且可能在运行时失败。
关于c# - 返回带有一个参数的元组作为泛型 : Tuple<bool, T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21395102/