2
votes

I have a type with a constructor taking two arguments:

type Foo = Foo of int * int

And I can call it with a tuple of (int, int).

let foo = Foo(1,2)

But in my case I want to partially apply the constructor to use it in a pipe

let someFunc param =
    calculateSomeInt param
    |> Foo 42  //error

But this is not possible because the constructor cannot be called in curried form.

Is there some possibility to tell F# I want the constructor in curried form without having to define a new function let fooCtr a b = Foo (a,b)?

2
Nope, constructor cannot be partially applied cause it is not F# function. This won't help much, but you could try (x,y) ||> Foo - Pavel Voronin
@PavelVoronin Actually just a normal pipe can be used here because Foo is a function that takes one tuple value: (42, param) |> Foo - TheQuickBrownFox
@TheQuickBrownFox Yes, you are right. - Pavel Voronin

2 Answers

6
votes

No, there isn't. The let fooCtr a b = Foo (a,b) approach you mentioned in your question is the only way to do this. (I would probably call it makeFoo or better yet mkFoo rather than fooCtr myself, since I feel that function names should be verbs, but that's personal style preference).

0
votes

rmunn's answer is correct, but I'd like to add a detail.

You can add the constructor as a parameter to the make function, so you can use make to partially apply any constructor which takes a pair (a 2-tuple) as parameter:

type TypeA =
    | A of int*int
    | B of int*string

type TypeB = 
    | C of int*float

// make : (('a * 'b -> 'c) -> 'a -> 'b -> 'c)
let make t a b = t(a,b)

// x : int -> TypeA
let x = make A 1

// y : string -> TypeA
let y = make B 2

// z : float -> TypeB
let z = make C 3

In fact, it works with any function which takes a pair as parameter, so the name make is slightly misleading in this case.