管道符号与对象构造函数一起使用

管道符号与对象构造函数一起使用

本文介绍了将 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.

更新构造函数是 F# 4.0 中的一等函数

UpdateConstructors 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# 管道符号与对象构造函数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:03