Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top