Skip to content

Instantly share code, notes, and snippets.

@JefClaes
Last active August 29, 2015 14:04
Show Gist options
  • Save JefClaes/26e159f61b06fc1712e6 to your computer and use it in GitHub Desktop.
Save JefClaes/26e159f61b06fc1712e6 to your computer and use it in GitHub Desktop.
module Program
open Inventory
open System
[<EntryPoint>]
let main argv =
let item = Inventory.Register (Inventory.InventoryItemName.Make "Domain Driven Design")
let itemAfterCheckingIn = Inventory.CheckIn (InventoryItemCount.Make 3) item
let itemAfterCheckingOut = Inventory.CheckOut (InventoryItemCount.Make 1) itemAfterCheckingIn
let deactivatedItem = Inventory.Deactivate itemAfterCheckingOut
let x = Console.ReadLine()
0 // return an integer exit code
module Inventory
open System
type InventoryItemName (value: string) =
member this.Value = value
static member Make(value: string) =
match value with
| "" -> invalidArg "value" "Name shouldn't be empty."
| _ -> InventoryItemName value
type InventoryItemCount(value: int) =
member this.Value = value
static member (-) (countLeft: InventoryItemCount, countRight: InventoryItemCount) =
InventoryItemCount.Make(countLeft.Value-countRight.Value)
static member (+) (countLeft: InventoryItemCount, countRight: InventoryItemCount) =
InventoryItemCount.Make(countLeft.Value+countRight.Value)
static member Empty =
InventoryItemCount.Make 0
static member Make(value: int) =
if value < 0 then
invalidArg "value" "Count can't be negative."
InventoryItemCount value
type InventoryItem = { Id: Guid; Activated: bool; Name: InventoryItemName; Count: InventoryItemCount; }
let Register name =
{ Id = Guid.NewGuid(); Activated = false; Name = name; Count = InventoryItemCount.Empty; }
let CheckIn count item =
{ item with Count = item.Count + count }
let CheckOut count item =
{ item with Count = item.Count - count }
let Deactivate item =
{ item with Activated = false }
@thinkbeforecoding
Copy link

Don't use tuples for functions arts !

let checkout count item = { ... }

@thinkbeforecoding
Copy link

And make inventoryItemCount a struct '

@JefClaes
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment