문제

I'm learning F# and I don't know how to achieve this.

I have an Interface:

type IRule =
    abstract member Getrule : keyfield:string -> bool * 'T

In C#:

public interface IRule
{
    Tuple<bool, T> GetRule<T>(string keyFieldIdentifier);
}

I need to create a Class that implement this 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)

I don't know how to write the last line (true, value :> 'a), how cast to generic type?

The error says:

Error 1 The static coercion from type int
to 'T
involves an indeterminate type based on information prior to this program point. Static coercions are not allowed on some types.

도움이 되었습니까?

해결책

This will compile - but I am not quite sure if it will do what you want

type SomeClass() =
    interface IRule with
        member this.Getrule<'T>(keyfield):bool * 'T =
            let value = 3
            (true,unbox box value )

In particular this doesn't do a cast and may fail at runtime.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top