Skip to content

Instantly share code, notes, and snippets.

@zah
Created September 12, 2011 11:41
Show Gist options
  • Select an option

  • Save zah/1211075 to your computer and use it in GitHub Desktop.

Select an option

Save zah/1211075 to your computer and use it in GitHub Desktop.
The case of unwanted copies
# While programming in nimrod, I often miss the reference types from c++
# Consider this example:
type CompositeObject = object
unwantedCopy: string
expensiveToCopy: list[matrix]
impossibleToCopy: map[string, TNetworkConnection]
proc properAccessor(o: var CompositeObject) : var string =
return o.unwantedCopy
proc clientCode =
...
var o : CompositeObject = ...
var s = o.properAccessor # unwanted copy here, I just want to read from the value
# The situation gets worse with the other more expensive fields.
# There will also be fields values for which defining a sensible copy semantics will
# be problematic (e.g. TNetworkConnection)
# Example 2: Using references to avoid repeating complex expressions
proc wordCounter =
...
var wordCountData = knownWords[word]
inc wordCountData.counter
wordCountData.addOccurrence(currentLine, currentColumn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment