F# three parameter infix operator

Viewed 111

How to use a three parametered infix operator?

Eg.: base function is let orElse labelFunc p1 p2 = {...} and operator let ( <|> ) = orElse

Now, for non-infix version this works nicely:List.reduce ((<|>) labelFunc) parserList.

Can I use it somehow still "infixing"? eg.: (p1 (<|> labelFunc) p1) does not work nor any other combination, other than using the non-infix version here as well.

2 Answers

First of all, I think it's best to restrict the number of custom operators you're using in your code, because custom operators make F# code hard to read. F# lets you define custom operators, but it's not particularly designed to make this a great experience - it makes sense for some small domain-specific languages (like parser combinators), but not much else.

So, while I do not recommend using this, there is a weird trick that you can use to write something like p1 (<op> l) p2, which is to make <op> infix and replace the parentheses with two more custom operators:

let (</) a b = a, b
let (/>) c d = c, d
let (!) f = f

1 </ !10 /> 2

This sample just produces a tuple with all three arguments, but if you implement your logic in the </ operator, it will actually do something like what you want. But as I said, I would not do this :-).

I don't believe there is any good way to achieve that. Once you have a parenthesized expression it won't be parsed as an operator - even 1 (+) 1 doesn't work.

Related