FZed
FZed

Reputation: 528

Defining an operator for the let statement (binding)

In F#, is it possible to define an operator, say (<<), that would assign a value to a variable like so :

let (<<) var value = ??<let var = value>??

that would compile inside the program and for which the resulting variable var of the let assignment could be used later in the program.

If it is possible, what is the syntax between the ?? delimiters ?

If not, what is the reason that would enlighten my knowledge of F# ?

Notes :

I suppose I cannot use Code Quotations in this case.

As a let binding, it is not a complete expression ; unlike other examples of operator definition (eg. (+)) I have seen.

Upvotes: 1

Views: 129

Answers (1)

Gus
Gus

Reputation: 26184

Yes, it's possible if you deal with variables, not bindings.

For example for ref cells you can do this:

let a = ref 5

let (<<) var value = var := value

a << 6

!a // returns 6

You are basically aliasing the (:=) operator.

For let bindings it's not possible, because they are immutable and let is more a keyword than a first-class function.

Upvotes: 2

Related Questions