相关疑难解决方法(0)

在C#中访问F#区分联合类型数据的最简单方法是什么?

我试图了解C#和F#可以一起玩的程度.我从F#for Fun&Profit博客中获取了一些代码,该博客执行基本验证,返回一个有区别的联合类型:

type Result<'TSuccess,'TFailure> = 
    | Success of 'TSuccess
    | Failure of 'TFailure

type Request = {name:string; email:string}

let TestValidate input =
    if input.name = "" then Failure "Name must not be blank"
    else Success input
Run Code Online (Sandbox Code Playgroud)

试图在C#中使用它时; 我能找到访问成功和失败的值的唯一方法(失败是一个字符串,成功再次是请求)是一个大讨厌的演员(这是很多打字,并需要输入我希望的实际类型推断或在元数据中可用):

var req = new DannyTest.Request("Danny", "fsfs");
var res = FSharpLib.DannyTest.TestValidate(req);

if (res.IsSuccess)
{
    Console.WriteLine("Success");
    var result = ((DannyTest.Result<DannyTest.Request, string>.Success)res).Item;
    // Result is the Request (as returned for Success)
    Console.WriteLine(result.email);
    Console.WriteLine(result.name);
}

if (res.IsFailure)
{
    Console.WriteLine("Failure");
    var result = …
Run Code Online (Sandbox Code Playgroud)

.net c# f# functional-programming

20
推荐指数
4
解决办法
4407
查看次数

C#中的FSharpChoice

我想FSharpChoice在C#项目中使用type.我创造了一个这样的选择

var a = FSharpChoice<T1,T2,T3>.NewChoice1Of3(instofT1);
Run Code Online (Sandbox Code Playgroud)

现在我如何instofT1摆脱选择类型.

我看到我可以做一个,IsChoice1Of3但我如何得到选择对象中的值?

c# f#

6
推荐指数
2
解决办法
1307
查看次数

在C#中使用F#类型

我有一个C#web api用于访问F#库.我创建了一个我想要返回的类型的DU,并使用模式匹配来选择哪个返回到c#控制器.

在C#控制器中,如何访问从函数调用返回到F#库的类型的数据?

C#控制器

public HttpResponseMessage Post()
{
    var _result = Authentication.GetAuthBehaviour();
    //Access item1 of my tuple
    var _HTTPStatusCode = (HttpStatusCode)_result.item1;
    //Access item2 of my tuple
    var _body = (HttpStatusCode)_result.item2;
    return base.Request.CreateResponse(_HTTPStatusCode, _body);
}
Run Code Online (Sandbox Code Playgroud)

F#类型

module Types =
    [<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
    [<CLIMutable>]
    type ValidResponse = {
         odata: string;
         token: string; 
    }

    [<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
    [<CLIMutable>]
    type ErrorResponse = {
         code: string;
         message: string;
         url: string; 
    }

    type AuthenticationResponse = 
    | Valid of int * ValidResponse
    | Error of int * ErrorResponse
Run Code Online (Sandbox Code Playgroud)

F#功能

module Authentication …
Run Code Online (Sandbox Code Playgroud)

c# f# types

5
推荐指数
1
解决办法
314
查看次数

如何在不限于一个文件的情况下在F#中实现访客模式?

以下代码示例演示了F#中访客模式的实现

module VisitorPattern

    type IVisitor =
        abstract Visit : ObjectA -> unit
        abstract Visit : ObjectB -> unit

    and IVisitable =
        abstract InvokeVisit : IVisitor -> unit

    and ObjectA =
        interface IVisitable with
            member this.InvokeVisit (visitor: IVisitor) =
                visitor.Visit(this)

    and ObjectB =
        interface IVisitable with
            member this.InvokeVisit (visitor: IVisitor) =
                visitor.Visit(this)

    type MyVisitor =
        member this.Visit (a : ObjectA) =
            printfn "Visited object A"

        member this.Visit (b : ObjectB) =
            printfn "Visited object B"
Run Code Online (Sandbox Code Playgroud)

这样可以很好地编译,但是IVisitable由于使用了and关键字,我们只能在一个文件中实现所有类型。该关键字似乎对于允许相互类型引用是必需的。

有没有一种方式可以实现这种模式,使我们不限于一个文件? …

f# visitor-pattern

4
推荐指数
1
解决办法
132
查看次数

标签 统计

f# ×4

c# ×3

.net ×1

functional-programming ×1

types ×1

visitor-pattern ×1