Created
September 12, 2011 11:41
-
-
Save zah/1211075 to your computer and use it in GitHub Desktop.
The case of unwanted copies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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