views:

351

answers:

2

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.

+4  A: 

There's always

(fun args -> new Shape(args))
Brian
Uhg, so simple. I tried so many different syntax's and didn't even think of using a fun. Thanks, sir.
gradbot
+2  A: 

Apparently, object constructors aren't composable. Discriminated union constructors don't seem to have this problem:

> 1 + 1 |> Some;;
val it : int option = Some 2

If you want to use the pipeline, Brian's answer is probably best. In this case, I'd consider just wrapping the entire expression with Shape( ).

MichaelGG
Cool, thanks for the clarification. I'm going to stick with Brian's way for now just for constancy sake. I like ending functions with easy to recognize types. Maybe they will add support for this in the future.
gradbot