Question

let tolerance = 0.00000001
let (~=) x1 x2 = abs(x1 - x2) < tolerance

This throws the error: "invalid operator definition. Prefix operator definitions must use a valid prefix operator name"

This isn't even a prefix operator, I don't get why it thinks so.

However the following is fine:

let (=~) x1 x2 = abs(x1 - x2) < tolerance

I only switched the order, so "=" comes before "~".

Is there any document online stating some rules regarding this?

I'm using Visual Studio 2013 with "F# 2013". The interactive console says "F# Interactive version 12.0.21005.1"

Was it helpful?

Solution

You can't define an infix operator starting with ~ in F#.

The F# 3.0 specification, section Categorization of Symbolic Operators explains the reason quite clearly:

The operators +, -, +., -., %, %%, &, && can be used as both prefix and infix operators. When these operators are used as prefix operators, the tilde character is prepended internally to generate the operator name so that the parser can distinguish such usage from an infix use of the operator. For example, -x is parsed as an application of the operator ~- to the identifier x. This generated name is also used in definitions for these prefix operators. Consequently, the definitions of the following prefix operators include the ~ character.

OTHER TIPS

In F#, the ~ character in first position denotes a prefix operator. For example, (~-) is the prefix "opposite" operator: (~-) 3 is equivalent to - 3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top