Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'and' keyword in F#

Tags:

f#

from the documentation at https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/keyword-reference:

enter image description here

it looks like 'and' can be used in the four categories above. With records and constraints, I see the use cases. But can anyone illustrate the uses in let bindings and members?

I see an example in this gist: https://gist.github.com/theburningmonk/3199252 but I'm not sure how it works.

like image 486
Thomas Avatar asked Jul 22 '20 19:07

Thomas


1 Answers

For let bindings and members, the and keyword is used for definining mutually recursive types and functions. A silly example with functions:

let rec f x = g x + 1
and g x = f x - 1

A silly example with classes:

type A() = 
  member x.B = B()
and B() = 
  member x.A = A()

The case with classes really covers all possible type definitions, including records and discriminated unions:

type A = 
  | Aaa of int 
  | Aaaa of C
and C = 
  { Bbb : B }
and B() = 
  member x.Bbb = Aaa 10
like image 92
Tomas Petricek Avatar answered Sep 20 '22 12:09

Tomas Petricek