patternMinor
F# inventory system - Part 2
Viewed 0 times
inventorysystempart
Problem
I've refactored my previous inventory system, and added a few features like removing items from the
Item.fs
Inventory.fs
```
namespace InventorySystem
open System
///
/// This type represents an inventory, containing items.
///
/// A list of items.
type Inventory(items: Item list) =
let mutable _items = items
let mutable _selectedItem = 0
member this.Items
with get() = _items
and set(value: Item list) = _items
/// Add an item to the list of items.
///
/// The item to add.
member this.AddItem(item: Item) =
this.
Inventory class, easily obtaining the current selected item through Inventory.GetSelectedItem, and some things have been renamed as well. My concerns are once again:- 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 mutable variables internalName in my types, or is this automatically done?
- Is my code properly styled?
Item.fs
namespace InventorySystem
open System
///
/// Represents an item. This type is only
/// with the Inventory type.
///
/// The item's name.
/// The amount of this specific item.
type Item(name: string, count: int) =
let mutable _count = count
let mutable _name = name
member this.Name
with get() = _name
and set(value: string) = _name
/// Increment or decrement the how much of this
/// specific item you have.
///
/// The amount to change by.
member this.ChangeCount(amount: int) =
if this.Count + amount >= 0 then
this.Count
/// Return this type in the following format:
/// Name={item name}, Count={item count}.
///
override this.ToString() =
String.Format("Name={0}, Count={1}", this.Name, this.Count)Inventory.fs
```
namespace InventorySystem
open System
///
/// This type represents an inventory, containing items.
///
/// A list of items.
type Inventory(items: Item list) =
let mutable _items = items
let mutable _selectedItem = 0
member this.Items
with get() = _items
and set(value: Item list) = _items
/// Add an item to the list of items.
///
/// The item to add.
member this.AddItem(item: Item) =
this.
Solution
There's a few small issues that I noticed with this.
The cons operator,
Although, since
In addition to that, the
The cons operator,
::, adds an item to the beginning of the list. I've done this in the Inventory.AddItem method. Since most people are used to items being added to the end of the list, the Inventory.AddItem method should be changed to this:member this.AddItem(item: Item) =
this.Items <- List.append [item] this.ItemsAlthough, since
List.append supports an entire 'T list as input, something like this can be done:member this.AddItems(items: Item list) =
this.Items <- List.append items this.ItemsIn addition to that, the
Item.ChangeCount method is completely useless. I can just refactor Item.Count's setter to something like this:member this.Count
with get() = _count
and set(value: int) =
if this.Count + value >= 0 then
this.Count <- this.Count + value
else
printfn "You cannot have less than zero items."Code Snippets
member this.AddItem(item: Item) =
this.Items <- List.append [item] this.Itemsmember this.AddItems(items: Item list) =
this.Items <- List.append items this.Itemsmember this.Count
with get() = _count
and set(value: int) =
if this.Count + value >= 0 then
this.Count <- this.Count + value
else
printfn "You cannot have less than zero items."Context
StackExchange Code Review Q#98087, answer score: 4
Revisions (0)
No revisions yet.