title: Working with classes in F# (mutability vs immutability / members vs free functions)
body: <p>I am currently doing the <a href="http://exercism.io/">exercism.io</a> F# track. For everyone who doesn't know it, it's solving small problems TDD style to learn or improve a programming language.</p>
  
  <p>The last two tasks were about the usage of classes in F# (or types as they are called in F#). One of the tasks uses a BankAccount that has a balance and a status (open/closed) and can be altered by using functions. The usage was like this (Taken from the test code):</p>
  
  <pre><code>let test () =
      let account = mkBankAccount () |&gt; openAccount
      Assert.That(getBalance account, Is.EqualTo(Some 0.0)
  </code></pre>
  
  <p>I wrote the code that makes the test pass using an immutable BankAccount class that can be interacted with using free functions:</p>
  
  <pre><code>type AccountStatus = Open | Closed
  
  type BankAccount (balance, status) =
      member acc.balance = balance
      member acc.status = status
  
  let mkBankAccount () =
      BankAccount (0.0, Closed)
  
  let getBalance (acc: BankAccount) =
      match acc.status with
      | Open -&gt; Some(acc.balance)
      | Closed -&gt; None
  
  let updateBalance balance (acc: BankAccount) =
      match acc.status with
      | Open -&gt; BankAccount (acc.balance + balance, Open)
      | Closed -&gt; failwith "Account is closed!"
  
  let openAccount (acc: BankAccount) =
      BankAccount (acc.balance, Open)
  
  let closeAccount (acc: BankAccount) =
      BankAccount (acc.balance, Closed)
  </code></pre>
  
  <p>Having done a lot of OO before starting to learn F# this one got me wondering. How do more experienced F# developers use classes? To make answering this question more simple, here are my main concerns about classes/types in F#:</p>
  
  <ul>
  <li>Is the use of classes in a typical OO fashion discouraged in F#?</li>
  <li>Are immutable classes preferred? ( I found them to be confusing in the above example)</li>
  <li>What is the preferred way to access/alter class data in F#? (Class member functions and get/set or free functions which allow piping? What about static members to allow piping and providing the functions with a fitting namespace?)</li>
  </ul>
  
  <p>I'm sorry if the question is vague. I don't want to develop bad coding habits in my functional code and i need a starting point on what good practices are.</p>