Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

What I want to do ...

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

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

Update Constructors are first-class functions as of 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
749 views
Welcome To Ask or Share your Answers For Others

1 Answer

There's always

(fun args -> new Shape(args))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...