Last active
July 28, 2025 16:28
-
-
Save naranyala/92f33c4d9f56d98d0a6eb5ee7db8a0c3 to your computer and use it in GitHub Desktop.
for the community, a getter setter implementation of nim programming
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
| type | |
| Person* = object | |
| name*: string | |
| # Getter and setter procedures | |
| proc getName*(p: Person): string = | |
| ## Getter for person's name | |
| p.name | |
| proc setName*(p: var Person, value: string): bool = | |
| ## Setter for person's name with validation | |
| if value.len == 0: | |
| return false # Validation failed | |
| p.name = value | |
| true # Validation succeeded | |
| when isMainModule: | |
| var person: Person | |
| if person.setName("Alice"): | |
| echo person.getName() # Outputs: Alice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment