patternMinor
F# inventory system
Viewed 0 times
inventorysystemstackoverflow
Problem
To learn F#, I've implemented this very simple inventory system. While I'm proud that that it's my first program, and that it works, there are still a few areas that I'd like tips on, namely these:
```
open System
///
/// Represents an item. This type is only
/// used in the Inventory type.
///
/// The item's name.
/// The amount of this specific item.
type InventoryItem(name:string, count:int) =
let mutable internalCount = count
member this.Name = name
member this.Count
with get() = internalCount
and set(value) = internalCount
/// Increment or decrement how many items there are.
/// It will not allow for a negative count.
///
/// The amount to change the item count by.
member this.ChangeCount(amount) =
if this.Count >= 1 then
this.Count
/// This type represents an inventory, a collection
/// of values of the InventoryItem type.
///
/// A list of InventoryItems.
///
type Inventory(items:InventoryItem list, selectedItem:int) =
let mutable internalItems = items
let mutable internalSelectedItem = selectedItem
member this.Items
with get() = internalItems
and set(value:InventoryItem list) = internalItems
/// Add an item to the inventory.
///
/// The item to add.
member this.AddItem(item:InventoryItem) =
this.Items
/// Change the currently selected item.
///
/// The amount to increase by.
member this.ChangeSelectedItem(amount:int) =
let mutable itemCount = -1;
for _ in this.Items do
itemCount
- I don't like how I'm using a
for ... in ...loop inInventory.ChangeSelectedItemto find the length ofInventory.Items.
- Is this the correct usage of F#'s type/class system? Should this be made in a more "functional" way?
- Am I using getters/setters correctly? Do I need to declare the
mutablevariablesinternalNamein mytypes, or is this automatically done?
- Is my code properly styled?
```
open System
///
/// Represents an item. This type is only
/// used in the Inventory type.
///
/// The item's name.
/// The amount of this specific item.
type InventoryItem(name:string, count:int) =
let mutable internalCount = count
member this.Name = name
member this.Count
with get() = internalCount
and set(value) = internalCount
/// Increment or decrement how many items there are.
/// It will not allow for a negative count.
///
/// The amount to change the item count by.
member this.ChangeCount(amount) =
if this.Count >= 1 then
this.Count
/// This type represents an inventory, a collection
/// of values of the InventoryItem type.
///
/// A list of InventoryItems.
///
type Inventory(items:InventoryItem list, selectedItem:int) =
let mutable internalItems = items
let mutable internalSelectedItem = selectedItem
member this.Items
with get() = internalItems
and set(value:InventoryItem list) = internalItems
/// Add an item to the inventory.
///
/// The item to add.
member this.AddItem(item:InventoryItem) =
this.Items
/// Change the currently selected item.
///
/// The amount to increase by.
member this.ChangeSelectedItem(amount:int) =
let mutable itemCount = -1;
for _ in this.Items do
itemCount
Solution
Some things:
can become
In terms of types, in F# I would probably make
The easiest way to know if something needs to be mutable and you are stuck is to leave it off and see if the compiler complains.
should probably be
let mutable itemCount = -1;
for _ in this.Items do
itemCount <- itemCount + 1can become
let itemCount = this.Items |> List.lengthIn terms of types, in F# I would probably make
Inventory Item a record rather than a class.The easiest way to know if something needs to be mutable and you are stuck is to leave it off and see if the compiler complains.
member this.ChangeCount(amount) =
if this.Count >= 1 then
this.Count <- this.Count + amountshould probably be
member this.ChangeCount(amount) =
if this.Count+amount >= 1 then
this.Count <- this.Count + amountCode Snippets
let mutable itemCount = -1;
for _ in this.Items do
itemCount <- itemCount + 1let itemCount = this.Items |> List.lengthmember this.ChangeCount(amount) =
if this.Count >= 1 then
this.Count <- this.Count + amountmember this.ChangeCount(amount) =
if this.Count+amount >= 1 then
this.Count <- this.Count + amountContext
StackExchange Code Review Q#97642, answer score: 7
Revisions (0)
No revisions yet.