4

I'm currently reading Real-World Functional Programming, and it briefly mentions in-fix operators as being one of the main benefits of custom operators. Are there any sort of standards for when to use or not use custom operators in F#? I'm looking for answers equivalent to this.

For reference, here is the quote to which @JohnPalmer is referring from here:

3.8 Operator Definitions 

Avoid defining custom symbolic operators in F#-facing library designs.

Custom operators are essential in some situations and are highly useful notational devices within a large body of implementation code. For new users of a library, named functions are often easier to use. In addition custom symbolic operators can be hard to document, and users find it more difficult to lookup help on operators, due to existing limitations in IDE and search engines.

As a result, it is generally best to publish your functionality as named functions and members.

Community
  • 1
  • 1
N_A
  • 19,799
  • 4
  • 52
  • 98
  • 2
    Why is this question not constructive? Knowing community standards on when to use/not use language features is important for writing readable code. And from the faq on what is a good question to ask: "practical, answerable problems that are unique to the programming profession" – N_A Mar 13 '13 at 04:19
  • According to microsoft, the answer is almost never: http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp-component-design-guidelines.pdf – John Palmer Mar 13 '13 at 04:32
  • @JohnPalmer Good find. It specifically says to not use them in F# facing _library_ design. I've updated my question with a bit more info. – N_A Mar 13 '13 at 04:40

1 Answers1

3

Custom infix operators are a nice feature in some situations, but when you use them, you have to be very careful to keep your code readable - so the recommendation from F# design guidelines applies most of the time. If I was writing Real-World Functional Programming again, I would be a bit less enthusiastic about them, because they really should be used carefuly :-).

That said, there are some F# libraries that make good use of custom operators and sometimes they work quite nicely. I think FParsec (parser combinator library) is one case - though maybe they have too many of them. Another example is a XML DSL which uses @=.

In general, when you're writing ordinary F# library, you probably do not want to expose them. However, when you're writing a domain specific language, custom operators might be useful.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • DSL do seem the area where they would cause the least amount of confusion. Thanks for the input! – N_A Mar 13 '13 at 12:50