gpt4 book ai didi

syntax - 将 F# 管道符号与对象构造函数一起使用

转载 作者:行者123 更新时间:2023-12-03 20:26:44 24 4
gpt4 key购买 nike

我试图找出使用管道运算符 |> 来创建对象的正确语法。目前我正在使用静态成员来创建对象,然后直接通过管道传输到该对象。这是简化版。

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 重复我的构造函数来复制代码。

更新
自 F# 4.0 起,构造函数是一流的函数

在 F# 4.0 中,正确的语法是。
    static member concat(shapes : Shape list) =
shapes
|> List.map (fun shape -> shape.points)
|> Array.concat
|> Shape

最佳答案

总有

(fun args -> new Shape(args))

关于syntax - 将 F# 管道符号与对象构造函数一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/531178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com