使用F#管道符号和对象构造函数 [英] Using the F# pipe symbol with an object constructor

查看:162
本文介绍了使用F#管道符号和对象构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出正确的语法使用管道操作符|>创建对象。目前,我使用一个静态成员来创建对象,只是管道。这是简化版本。

I'm trying to figure out the correct syntax to use the pipe operator |> into the creation of an object. Currently I'm using a static member to create the object and just piping to that. Here is the simplified version.

type Shape = 
    val points : Vector[]

    new (points) =
        { points = points; }

    static member create(points) =
        Shape(points)

    static member concat(shapes : Shape list) =
        shapes
            |> List.map (fun shape -> shape.points)
            |> Array.concat
            |> Shape.create

我想做什么...

    static member concat(shapes : Shape list) =
        shapes
            |> List.map (fun shape -> shape.points)
            |> Array.concat
            |> (new Shape)

这样的东西可能吗?我不想通过使用静态成员create重复我的构造函数来重复代码。

Is something like this possible? I don't want to duplicate code by repeating my constructor with the static member create.

更新
构造函数是first class函数F#4.0

Update Constructors are first-class functions as of F# 4.0

在F#4.0中,正确的语法是

In F# 4.0 the correct syntax is.

    static member concat(shapes : Shape list) =
        shapes
            |> List.map (fun shape -> shape.points)
            |> Array.concat
            |> Shape


推荐答案

总是

(fun args -> new Shape(args))

这篇关于使用F#管道符号和对象构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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