相关疑难解决方法(0)

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

我试图找出使用管道运算符|>进入对象创建的正确语法.目前我正在使用静态成员来创建对象,并且只是为了管道.这是简化版.

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
Run Code Online (Sandbox Code Playgroud)

我想做的事 ...

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

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

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

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

    static member concat(shapes : Shape list) =
        shapes
            |> List.map (fun shape -> shape.points)
            |> Array.concat …
Run Code Online (Sandbox Code Playgroud)

syntax f# constructor pipe

15
推荐指数
1
解决办法
1928
查看次数

标签 统计

constructor ×1

f# ×1

pipe ×1

syntax ×1